git://git.nikiroo.be
/
nikiroo-utils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
fix whitelist mode on web lib
[nikiroo-utils.git]
/
src
/
be
/
nikiroo
/
fanfix
/
library
/
RemoteLibrary.java
diff --git
a/src/be/nikiroo/fanfix/library/RemoteLibrary.java
b/src/be/nikiroo/fanfix/library/RemoteLibrary.java
index 077a857cf9a748ddbd242b1af625b853ecf6d75e..9fa8c66190174bd1a92536b995d181a16bc8744a 100644
(file)
--- a/
src/be/nikiroo/fanfix/library/RemoteLibrary.java
+++ b/
src/be/nikiroo/fanfix/library/RemoteLibrary.java
@@
-21,6
+21,8
@@
import be.nikiroo.utils.serial.server.ConnectActionClientObject;
* This {@link BasicLibrary} will access a remote server to list the available
* stories, and download the ones you try to load to the local directory
* specified in the configuration.
* This {@link BasicLibrary} will access a remote server to list the available
* stories, and download the ones you try to load to the local directory
* specified in the configuration.
+ * <p>
+ * This remote library uses a custom fanfix:// protocol.
*
* @author niki
*/
*
* @author niki
*/
@@
-35,18
+37,13
@@
public class RemoteLibrary extends BasicLibrary {
}
@Override
}
@Override
- public Object send(Object data)
throws IOException,
- NoSuchFieldException, NoSuchMethodException,
+ public Object send(Object data)
+
throws IOException,
NoSuchFieldException, NoSuchMethodException,
ClassNotFoundException {
Object rep = super.send(data);
if (rep instanceof RemoteLibraryException) {
RemoteLibraryException remoteEx = (RemoteLibraryException) rep;
ClassNotFoundException {
Object rep = super.send(data);
if (rep instanceof RemoteLibraryException) {
RemoteLibraryException remoteEx = (RemoteLibraryException) rep;
- IOException cause = remoteEx.getCause();
- if (cause == null) {
- cause = new IOException("IOException");
- }
-
- throw cause;
+ throw remoteEx.unwrapException();
}
return rep;
}
return rep;
@@
-61,8
+58,6
@@
public class RemoteLibrary extends BasicLibrary {
// informative only (server will make the actual checks)
private boolean rw;
// informative only (server will make the actual checks)
private boolean rw;
- // TODO: error handling is not up to par!
-
/**
* Create a {@link RemoteLibrary} linked to the given server.
* <p>
/**
* Create a {@link RemoteLibrary} linked to the given server.
* <p>
@@
-85,7
+80,7
@@
public class RemoteLibrary extends BasicLibrary {
* <li><b>wl</b>: flag to allow access to all the stories (bypassing the
* whitelist if it exists)</li>
* </ul>
* <li><b>wl</b>: flag to allow access to all the stories (bypassing the
* whitelist if it exists)</li>
* </ul>
- *
+ *
<p>
* Some examples:
* <ul>
* <li><b>my_key</b>: normal connection, will take the default server
* Some examples:
* <ul>
* <li><b>my_key</b>: normal connection, will take the default server
@@
-120,20
+115,26
@@
public class RemoteLibrary extends BasicLibrary {
this.subkey = "";
}
this.subkey = "";
}
+ if (host.startsWith("fanfix://")) {
+ host = host.substring("fanfix://".length());
+ }
+
this.host = host;
this.port = port;
}
@Override
public String getLibraryName() {
this.host = host;
this.port = port;
}
@Override
public String getLibraryName() {
- return host + ":" + port;
+ return
(rw ? "[READ-ONLY] " : "") + "fanfix://" +
host + ":" + port;
}
@Override
public Status getStatus() {
}
@Override
public Status getStatus() {
- Instance.getTraceHandler().trace("Getting remote lib status...");
+ Instance.getInstance().getTraceHandler()
+ .trace("Getting remote lib status...");
Status status = getStatusDo();
Status status = getStatusDo();
- Instance.getTraceHandler().trace("Remote lib status: " + status);
+ Instance.getInstance().getTraceHandler()
+ .trace("Remote lib status: " + status);
return status;
}
return status;
}
@@
-150,10
+151,10
@@
public class RemoteLibrary extends BasicLibrary {
if ("r/w".equals(rep)) {
rw = true;
if ("r/w".equals(rep)) {
rw = true;
- result[0] = Status.READ
Y
;
+ result[0] = Status.READ
_WRITE
;
} else if ("r/o".equals(rep)) {
rw = false;
} else if ("r/o".equals(rep)) {
rw = false;
- result[0] = Status.READY;
+ result[0] = Status.READ
_ONL
Y;
} else {
result[0] = Status.UNAUTHORIZED;
}
} else {
result[0] = Status.UNAUTHORIZED;
}
@@
-180,15
+181,15
@@
public class RemoteLibrary extends BasicLibrary {
}
@Override
}
@Override
- public Image getCover(final String luid) {
+ public Image getCover(final String luid)
throws IOException
{
final Image[] result = new Image[1];
connectRemoteAction(new RemoteAction() {
@Override
public void action(ConnectActionClientObject action)
throws Exception {
final Image[] result = new Image[1];
connectRemoteAction(new RemoteAction() {
@Override
public void action(ConnectActionClientObject action)
throws Exception {
- Object rep = action
.send(new Object[] { subkey, "GET_COVER",
- luid });
+ Object rep = action
+
.send(new Object[] { subkey, "GET_COVER",
luid });
result[0] = (Image) rep;
}
});
result[0] = (Image) rep;
}
});
@@
-197,17
+198,18
@@
public class RemoteLibrary extends BasicLibrary {
}
@Override
}
@Override
- public Image getCustomSourceCover(final String source) {
+ public Image getCustomSourceCover(final String source)
throws IOException
{
return getCustomCover(source, "SOURCE");
}
@Override
return getCustomCover(source, "SOURCE");
}
@Override
- public Image getCustomAuthorCover(final String author) {
+ public Image getCustomAuthorCover(final String author)
throws IOException
{
return getCustomCover(author, "AUTHOR");
}
// type: "SOURCE" or "AUTHOR"
return getCustomCover(author, "AUTHOR");
}
// type: "SOURCE" or "AUTHOR"
- private Image getCustomCover(final String source, final String type) {
+ private Image getCustomCover(final String source, final String type)
+ throws IOException {
final Image[] result = new Image[1];
connectRemoteAction(new RemoteAction() {
final Image[] result = new Image[1];
connectRemoteAction(new RemoteAction() {
@@
-224,7
+226,8
@@
public class RemoteLibrary extends BasicLibrary {
}
@Override
}
@Override
- public synchronized Story getStory(final String luid, Progress pg) {
+ public synchronized Story getStory(final String luid, Progress pg)
+ throws IOException {
final Progress pgF = pg;
final Story[] result = new Story[1];
final Progress pgF = pg;
final Story[] result = new Story[1];
@@
-237,8
+240,8
@@
public class RemoteLibrary extends BasicLibrary {
pg = new Progress();
}
pg = new Progress();
}
- Object rep = action
.send(new Object[] { subkey, "GET_STORY",
- luid });
+ Object rep = action
+
.send(new Object[] { subkey, "GET_STORY",
luid });
MetaData meta = null;
if (rep instanceof MetaData) {
MetaData meta = null;
if (rep instanceof MetaData) {
@@
-331,18
+334,20
@@
public class RemoteLibrary extends BasicLibrary {
}
@Override
}
@Override
- public void setSourceCover(final String source, final String luid) {
+ public void setSourceCover(final String source, final String luid)
+ throws IOException {
setCover(source, luid, "SOURCE");
}
@Override
setCover(source, luid, "SOURCE");
}
@Override
- public void setAuthorCover(final String author, final String luid) {
+ public void setAuthorCover(final String author, final String luid)
+ throws IOException {
setCover(author, luid, "AUTHOR");
}
// type = "SOURCE" | "AUTHOR"
private void setCover(final String value, final String luid,
setCover(author, luid, "AUTHOR");
}
// type = "SOURCE" | "AUTHOR"
private void setCover(final String value, final String luid,
- final String type) {
+ final String type)
throws IOException
{
connectRemoteAction(new RemoteAction() {
@Override
public void action(ConnectActionClientObject action)
connectRemoteAction(new RemoteAction() {
@Override
public void action(ConnectActionClientObject action)
@@
-355,8
+360,9
@@
public class RemoteLibrary extends BasicLibrary {
@Override
// Could work (more slowly) without it
@Override
// Could work (more slowly) without it
- public
Story
imprt(final URL url, Progress pg) throws IOException {
+ public
MetaData
imprt(final URL url, Progress pg) throws IOException {
// Import the file locally if it is actually a file
// Import the file locally if it is actually a file
+
if (url == null || url.getProtocol().equalsIgnoreCase("file")) {
return super.imprt(url, pg);
}
if (url == null || url.getProtocol().equalsIgnoreCase("file")) {
return super.imprt(url, pg);
}
@@
-367,13
+373,7
@@
public class RemoteLibrary extends BasicLibrary {
pg = new Progress();
}
pg = new Progress();
}
- pg.setMinMax(0, 2);
- Progress pgImprt = new Progress();
- Progress pgGet = new Progress();
- pg.addProgress(pgImprt, 1);
- pg.addProgress(pgGet, 1);
-
- final Progress pgF = pgImprt;
+ final Progress pgF = pg;
final String[] luid = new String[1];
connectRemoteAction(new RemoteAction() {
final String[] luid = new String[1];
connectRemoteAction(new RemoteAction() {
@@
-382,8
+382,8
@@
public class RemoteLibrary extends BasicLibrary {
throws Exception {
Progress pg = pgF;
throws Exception {
Progress pg = pgF;
- Object rep = action.send(
new Object[] { subkey, "IMPORT",
- url.toString() });
+ Object rep = action.send(
+
new Object[] { subkey, "IMPORT",
url.toString() });
while (true) {
if (!RemoteLibraryServer.updateProgress(pg, rep)) {
while (true) {
if (!RemoteLibraryServer.updateProgress(pg, rep)) {
@@
-402,11
+402,8
@@
public class RemoteLibrary extends BasicLibrary {
throw new IOException("Remote failure");
}
throw new IOException("Remote failure");
}
- Story story = getStory(luid[0], pgGet);
- pgGet.done();
-
pg.done();
pg.done();
- return
story
;
+ return
getInfo(luid[0])
;
}
@Override
}
@Override
@@
-437,26
+434,32
@@
public class RemoteLibrary extends BasicLibrary {
}
@Override
}
@Override
- public
synchronized
File getFile(final String luid, Progress pg) {
+ public File getFile(final String luid, Progress pg) {
throw new java.lang.InternalError(
"Operation not supportorted on remote Libraries");
}
/**
* Stop the server.
throw new java.lang.InternalError(
"Operation not supportorted on remote Libraries");
}
/**
* Stop the server.
+ *
+ * @throws IOException
+ * in case of I/O errors
+ * @throws SSLException
+ * when the key was not accepted
*/
*/
- public void exit() {
+ public void exit()
throws IOException, SSLException
{
connectRemoteAction(new RemoteAction() {
@Override
public void action(ConnectActionClientObject action)
throws Exception {
action.send(new Object[] { subkey, "EXIT" });
connectRemoteAction(new RemoteAction() {
@Override
public void action(ConnectActionClientObject action)
throws Exception {
action.send(new Object[] { subkey, "EXIT" });
+ Thread.sleep(100);
}
});
}
@Override
}
});
}
@Override
- public
synchronized MetaData getInfo(String luid)
{
+ public
MetaData getInfo(String luid) throws IOException
{
List<MetaData> metas = getMetasList(luid, null);
if (!metas.isEmpty()) {
return metas.get(0);
List<MetaData> metas = getMetasList(luid, null);
if (!metas.isEmpty()) {
return metas.get(0);
@@
-466,7
+469,7
@@
public class RemoteLibrary extends BasicLibrary {
}
@Override
}
@Override
- protected List<MetaData> getMetas(Progress pg) {
+ protected List<MetaData> getMetas(Progress pg)
throws IOException
{
return getMetasList("*", pg);
}
return getMetasList("*", pg);
}
@@
-510,10
+513,13
@@
public class RemoteLibrary extends BasicLibrary {
* @param pg
* the optional progress
*
* @param pg
* the optional progress
*
- *
* @return the metas
* @return the metas
+ *
+ * @throws IOException
+ * in case of I/O error or bad key (SSLException)
*/
*/
- private List<MetaData> getMetasList(final String luid, Progress pg) {
+ private List<MetaData> getMetasList(final String luid, Progress pg)
+ throws IOException {
final Progress pgF = pg;
final List<MetaData> metas = new ArrayList<MetaData>();
final Progress pgF = pg;
final List<MetaData> metas = new ArrayList<MetaData>();
@@
-526,8
+532,8
@@
public class RemoteLibrary extends BasicLibrary {
pg = new Progress();
}
pg = new Progress();
}
- Object rep = action
.send(new Object[] { subkey, "GET_METADATA",
- luid });
+ Object rep = action
+
.send(new Object[] { subkey, "GET_METADATA",
luid });
while (true) {
if (!RemoteLibraryServer.updateProgress(pg, rep)) {
while (true) {
if (!RemoteLibraryServer.updateProgress(pg, rep)) {
@@
-550,7
+556,9
@@
public class RemoteLibrary extends BasicLibrary {
return metas;
}
return metas;
}
- private void connectRemoteAction(final RemoteAction runAction) {
+ private void connectRemoteAction(final RemoteAction runAction)
+ throws IOException {
+ final IOException[] err = new IOException[1];
try {
final RemoteConnectAction[] array = new RemoteConnectAction[1];
RemoteConnectAction ra = new RemoteConnectAction() {
try {
final RemoteConnectAction[] array = new RemoteConnectAction[1];
RemoteConnectAction ra = new RemoteConnectAction() {
@@
-561,18
+569,22
@@
public class RemoteLibrary extends BasicLibrary {
@Override
protected void onError(Exception e) {
@Override
protected void onError(Exception e) {
- if (e instanceof SSLException) {
- Instance.getTraceHandler().error(
- "Connection refused (bad key)");
- } else {
- Instance.getTraceHandler().error(e);
+ if (!(e instanceof IOException)) {
+ Instance.getInstance().getTraceHandler().error(e);
+ return;
}
}
+
+ err[0] = (IOException) e;
}
};
array[0] = ra;
ra.connect();
} catch (Exception e) {
}
};
array[0] = ra;
ra.connect();
} catch (Exception e) {
- Instance.getTraceHandler().error(e);
+ err[0] = (IOException) e;
+ }
+
+ if (err[0] != null) {
+ throw err[0];
}
}
}
}
}
}