From: SVN c. m. f. t. SWORD-A. p. <swo...@li...> - 2011-05-21 18:49:28
|
Revision: 321 http://sword-app.svn.sourceforge.net/sword-app/?rev=321&view=rev Author: richard-jones Date: 2011-05-21 18:49:21 +0000 (Sat, 21 May 2011) Log Message: ----------- better support for various status code responses from the server, and spec coverage up to section 8 Modified Paths: -------------- JavaClient2.0/src/main/java/org/swordapp/client/DepositReceipt.java JavaClient2.0/src/main/java/org/swordapp/client/SWORDClient.java JavaClient2.0/src/main/java/org/swordapp/client/SWORDMultipartRequestEntity.java Added Paths: ----------- JavaClient2.0/src/main/java/org/swordapp/client/ErrorHandler.java JavaClient2.0/src/main/java/org/swordapp/client/ResponseCodeManager.java JavaClient2.0/src/main/java/org/swordapp/client/ResponseStatus.java Modified: JavaClient2.0/src/main/java/org/swordapp/client/DepositReceipt.java =================================================================== --- JavaClient2.0/src/main/java/org/swordapp/client/DepositReceipt.java 2011-05-16 08:19:19 UTC (rev 320) +++ JavaClient2.0/src/main/java/org/swordapp/client/DepositReceipt.java 2011-05-21 18:49:21 UTC (rev 321) @@ -32,6 +32,11 @@ this.entry = entry; } + public DepositReceipt(int status, String location) + { + super(status, location); + } + public Entry getEntry() { return entry; Added: JavaClient2.0/src/main/java/org/swordapp/client/ErrorHandler.java =================================================================== --- JavaClient2.0/src/main/java/org/swordapp/client/ErrorHandler.java (rev 0) +++ JavaClient2.0/src/main/java/org/swordapp/client/ErrorHandler.java 2011-05-21 18:49:21 UTC (rev 321) @@ -0,0 +1,30 @@ +package org.swordapp.client; + +import org.apache.abdera.protocol.client.ClientResponse; + +public class ErrorHandler +{ + public SWORDError handleError(ClientResponse resp) + { + // find out what kind of error we got + int status = resp.getStatus(); + + switch (status) + { + case 401 : this.authorisationError(resp); + case 403 : this.forbidden(resp); + } + + return null; + } + + public void authorisationError(ClientResponse resp) + { + + } + + public void forbidden(ClientResponse resp) + { + + } +} Added: JavaClient2.0/src/main/java/org/swordapp/client/ResponseCodeManager.java =================================================================== --- JavaClient2.0/src/main/java/org/swordapp/client/ResponseCodeManager.java (rev 0) +++ JavaClient2.0/src/main/java/org/swordapp/client/ResponseCodeManager.java 2011-05-21 18:49:21 UTC (rev 321) @@ -0,0 +1,286 @@ +package org.swordapp.client; + +public class ResponseCodeManager +{ + public ResponseStatus depositNew(int status) + { + ResponseStatus rs = new ResponseStatus(); + + // correct response + if (status == 201) + { + rs.setCorrect(true); + } + + // incorrect but allowable + if (status == 200 || status == 202 || status == 203 || status == 204) + { + rs.setIncorrectButAllowed(true); + } + + // incorrect and violation + if (status == 205 || status == 206 || (status >=300 && status < 400)) + { + rs.setIncorrectAndViolation(true); + } + + // error + if (status >= 400) + { + rs.setError(true); + } + + // otherwise, was below 200, which shouldn't happen + return rs; + } + + public ResponseStatus getDepositReceipt(int status) + { + ResponseStatus rs = new ResponseStatus(); + + // correct response + if (status == 200) + { + rs.setCorrect(true); + } + + // incorrect but allowable + if (status == 203 || (status >= 300 && status < 400)) + { + rs.setIncorrectButAllowed(true); + } + + // incorrect and violation + if (status == 201 || status == 202 || status == 204 || status == 205 || status == 206) + { + rs.setIncorrectAndViolation(true); + } + + // error + if (status >= 400) + { + rs.setError(true); + } + + // otherwise, was below 200, which shouldn't happen + return rs; + } + + public ResponseStatus replaceMedia(int status) + { + ResponseStatus rs = new ResponseStatus(); + + // correct response + if (status == 204) + { + rs.setCorrect(true); + } + + // incorrect but allowable + if (status == 200 || status == 201 || status == 202 || status == 203) + { + rs.setIncorrectButAllowed(true); + } + + // incorrect and violation + if (status == 205 || status == 206 || (status >= 300 && status < 400)) + { + rs.setIncorrectAndViolation(true); + } + + // error + if (status >= 400) + { + rs.setError(true); + } + + // otherwise, was below 200, which shouldn't happen + return rs; + } + + public ResponseStatus replace(int status) + { + ResponseStatus rs = new ResponseStatus(); + + // correct response + if (status == 204 || status == 200) + { + rs.setCorrect(true); + } + + // incorrect but allowable + if (status == 201 || status == 202 || status == 203) + { + rs.setIncorrectButAllowed(true); + } + + // incorrect and violation + if (status == 205 || status == 206 || (status >= 300 && status < 400)) + { + rs.setIncorrectAndViolation(true); + } + + // error + if (status >= 400) + { + rs.setError(true); + } + + // otherwise, was below 200, which shouldn't happen + return rs; + } + + public ResponseStatus delete(int status) + { + ResponseStatus rs = new ResponseStatus(); + + // correct response + if (status == 204) + { + rs.setCorrect(true); + } + + // incorrect but allowable + if (status == 200 || status == 202 || status == 203) + { + rs.setIncorrectButAllowed(true); + } + + // incorrect and violation + if (status == 201 || status == 205 || status == 206 || (status >= 300 && status < 400)) + { + rs.setIncorrectAndViolation(true); + } + + // error + if (status >= 400) + { + rs.setError(true); + } + + // otherwise, was below 200, which shouldn't happen + return rs; + } + + public ResponseStatus addToMediaResource(int status) + { + ResponseStatus rs = new ResponseStatus(); + + // correct response + if (status >= 200 && status < 300) + { + rs.setCorrect(true); + } + + // incorrect and violation + if (status >= 300 && status < 400) + { + rs.setIncorrectAndViolation(true); + } + + // error + if (status >= 400) + { + rs.setError(true); + } + + // otherwise, was below 200, which shouldn't happen + return rs; + } + + public ResponseStatus addToContainer(int status) + { + ResponseStatus rs = new ResponseStatus(); + + // correct response + if (status == 200) + { + rs.setCorrect(true); + } + + // incorrect but allowable + if (status == 201 || status == 204 || status == 202 || status == 203) + { + rs.setIncorrectButAllowed(true); + } + + // incorrect and violation + if (status == 205 || status == 206 || (status >= 300 && status < 400)) + { + rs.setIncorrectAndViolation(true); + } + + // error + if (status >= 400) + { + rs.setError(true); + } + + // otherwise, was below 200, which shouldn't happen + return rs; + } + + public ResponseStatus getStatement(int status) + { + ResponseStatus rs = new ResponseStatus(); + + // correct response + if (status == 200) + { + rs.setCorrect(true); + } + + // incorrect but allowable + if (status == 203 || (status >= 300 && status < 400)) + { + rs.setIncorrectButAllowed(true); + } + + // incorrect and violation + if (status == 201 || status == 202 || status == 205 || status == 206 || status == 204) + { + rs.setIncorrectAndViolation(true); + } + + // error + if (status >= 400) + { + rs.setError(true); + } + + // otherwise, was below 200, which shouldn't happen + return rs; + } + + public ResponseStatus getContent(int status) + { + ResponseStatus rs = new ResponseStatus(); + + // correct response + if (status == 200) + { + rs.setCorrect(true); + } + + // incorrect but allowable + if (status == 203 || (status >= 300 && status < 400)) + { + rs.setIncorrectButAllowed(true); + } + + // incorrect and violation + if (status == 201 || status == 202 || status == 205 || status == 206 || status == 204) + { + rs.setIncorrectAndViolation(true); + } + + // error + if (status >= 400) + { + rs.setError(true); + } + + // otherwise, was below 200, which shouldn't happen + return rs; + } +} Added: JavaClient2.0/src/main/java/org/swordapp/client/ResponseStatus.java =================================================================== --- JavaClient2.0/src/main/java/org/swordapp/client/ResponseStatus.java (rev 0) +++ JavaClient2.0/src/main/java/org/swordapp/client/ResponseStatus.java 2011-05-21 18:49:21 UTC (rev 321) @@ -0,0 +1,49 @@ +package org.swordapp.client; + +public class ResponseStatus +{ + boolean correct = false; + boolean incorrectButAllowed = false; + boolean incorrectAndViolation = false; + boolean error = false; + + public boolean isCorrect() + { + return correct; + } + + public void setCorrect(boolean correct) + { + this.correct = correct; + } + + public boolean isIncorrectButAllowed() + { + return incorrectButAllowed; + } + + public void setIncorrectButAllowed(boolean incorrectButAllowed) + { + this.incorrectButAllowed = incorrectButAllowed; + } + + public boolean isIncorrectAndViolation() + { + return incorrectAndViolation; + } + + public void setIncorrectAndViolation(boolean incorrectAndViolation) + { + this.incorrectAndViolation = incorrectAndViolation; + } + + public boolean isError() + { + return error; + } + + public void setError(boolean error) + { + this.error = error; + } +} Modified: JavaClient2.0/src/main/java/org/swordapp/client/SWORDClient.java =================================================================== --- JavaClient2.0/src/main/java/org/swordapp/client/SWORDClient.java 2011-05-16 08:19:19 UTC (rev 320) +++ JavaClient2.0/src/main/java/org/swordapp/client/SWORDClient.java 2011-05-21 18:49:21 UTC (rev 321) @@ -200,6 +200,12 @@ public DepositReceipt deposit(SWORDCollection collection, Deposit deposit, AuthCredentials auth) throws SWORDClientException, SWORDError, ProtocolViolationException { + if (!collection.allowsMediation() && auth.getOnBehalfOf() != null) + { + log.warn("About to attempt to do mediated deposit onto a collection which does not support" + + " mediation; an error in response is likely. Provide an AuthCredentials object with " + + "valid contents on next run"); + } return this.deposit(collection.getHref().toString(), deposit, auth); } @@ -297,44 +303,37 @@ } int status = resp.getStatus(); - if (status == 201) + ResponseCodeManager rcm = new ResponseCodeManager(); + ResponseStatus rs = rcm.depositNew(status); + if (rs.isCorrect() || rs.isIncorrectButAllowed()) { log.info("Deposit request on " + url.toString() + " returned HTTP status " + status + "; SUCCESS"); + if (rs.isIncorrectButAllowed()) + { + log.warn("Server responded with status " + status + " which is incorrect. Attempting to continue " + + "processing response ..."); + } + // SUCCESS - DepositReceipt receipt; - Document<Entry> doc = resp.getDocument(); - String location = http.getLocation(resp); - - // it is possible that the doc will be null - // if there is no doc and no location header this is broken - if (doc == null && (location == null || "".equals(location))) - { - throw new ProtocolViolationException("SWORD Server responded 201 (Created) but failed to provide a deposit receipt or a Location header"); - } - - // if there is no doc, we need to see what the configuration asks us to do about getting it - if (doc == null && this.config.returnDepositReceipt()) - { - receipt = this.getDepositReceipt(location); - } - else - { - Entry entry = doc.getRoot(); - receipt = new DepositReceipt(status, location, entry); - } - + DepositReceipt receipt = this.getDepositReceipt(resp, auth); return receipt; } - else + else if (rs.isIncorrectAndViolation()) + { + throw new ProtocolViolationException("Server responded with invalid status " + status); + } + else if (rs.isError()) { // FIXME: this needs to handle all the other possible response codes - log.info("Deposit request on " + url.toString() + " return HTTP status " + status); - this.handleError(); + log.info("Deposit request on " + url.toString() + " returned Error HTTP status " + status); + ErrorHandler eh = new ErrorHandler(); + throw eh.handleError(resp); } - - // FIXME: we might want to error out here if we haven't already returned - return null; + else + { + throw new ProtocolViolationException("Unexpected response code " + status); + } } public SwordResponse replaceMedia(DepositReceipt receipt, Deposit deposit, AuthCredentials auth) @@ -411,24 +410,39 @@ log.debug("Successfully completed Media replace request (doesn't mean the replace was successful!) on url: " + url.toString()); } - int status = resp.getStatus(); - if (status == 204) + int status = resp.getStatus(); + ResponseCodeManager rcm = new ResponseCodeManager(); + ResponseStatus rs = rcm.replaceMedia(status); + if (rs.isCorrect() || rs.isIncorrectButAllowed()) { log.info("Replace request on " + url.toString() + " returned HTTP status " + status + "; SUCCESS"); + if (rs.isIncorrectButAllowed()) + { + log.warn("Server responded with status " + status + " which is incorrect. Attempting to continue " + + "processing response ..."); + } + + // SUCCESS // we just return an empty response SwordResponse receipt = new SwordResponse(status); return receipt; } - else + else if (rs.isIncorrectAndViolation()) + { + throw new ProtocolViolationException("Server responded with invalid status " + status); + } + else if (rs.isError()) { // FIXME: this needs to handle all the other possible response codes - log.info("Replace request on " + url.toString() + " return HTTP status " + status); - this.handleError(); + log.info("Replace request on " + url.toString() + " returned Error HTTP status " + status); + ErrorHandler eh = new ErrorHandler(); + throw eh.handleError(resp); } - - // FIXME: we might want to error out here if we haven't already returned - return null; + else + { + throw new ProtocolViolationException("Unexpected response code " + status); + } } public SwordResponse replace(String editURL, Deposit deposit, AuthCredentials auth) @@ -502,27 +516,43 @@ throw new SWORDClientException("Deposit Object does not have one/both of entry/content set"); } - int status = resp.getStatus(); - if (status == 200 || status == 204) + int status = resp.getStatus(); + ResponseCodeManager rcm = new ResponseCodeManager(); + ResponseStatus rs = rcm.replace(status); + if (rs.isCorrect() || rs.isIncorrectButAllowed()) { log.info("Replace request on " + url.toString() + " returned HTTP status " + status + "; SUCCESS"); + if (rs.isIncorrectButAllowed()) + { + log.warn("Server responded with status " + status + " which is incorrect. Attempting to continue " + + "processing response ..."); + } + + // SUCCESS + // we just return an empty response SwordResponse receipt = new SwordResponse(status); return receipt; } - else + else if (rs.isIncorrectAndViolation()) + { + throw new ProtocolViolationException("Server responded with invalid status " + status); + } + else if (rs.isError()) { // FIXME: this needs to handle all the other possible response codes - log.info("Replace request on " + url.toString() + " return HTTP status " + status); - this.handleError(); + log.info("Replace request on " + url.toString() + " returned Error HTTP status " + status); + ErrorHandler eh = new ErrorHandler(); + throw eh.handleError(resp); } - - // FIXME: we might want to error out here if we haven't already returned - return null; + else + { + throw new ProtocolViolationException("Unexpected response code " + status); + } } public SwordResponse delete(String deleteUrl, AuthCredentials auth) - throws SWORDClientException, SWORDError + throws SWORDClientException, SWORDError, ProtocolViolationException { // some initial error checking and validation if (deleteUrl == null) @@ -555,26 +585,42 @@ ClientResponse resp = client.delete(url.toString(), options); int status = resp.getStatus(); - if (status == 204) + ResponseCodeManager rcm = new ResponseCodeManager(); + ResponseStatus rs = rcm.delete(status); + if (rs.isCorrect() || rs.isIncorrectButAllowed()) { - log.info("Replace request on " + url.toString() + " returned HTTP status " + status + "; SUCCESS"); + log.info("Delete request on " + url.toString() + " returned HTTP status " + status + "; SUCCESS"); + if (rs.isIncorrectButAllowed()) + { + log.warn("Server responded with status " + status + " which is incorrect. Attempting to continue " + + "processing response ..."); + } + + // SUCCESS + // we just return an empty response SwordResponse receipt = new SwordResponse(status); return receipt; } - else + else if (rs.isIncorrectAndViolation()) + { + throw new ProtocolViolationException("Server responded with invalid status " + status); + } + else if (rs.isError()) { // FIXME: this needs to handle all the other possible response codes - log.info("Replace request on " + url.toString() + " return HTTP status " + status); - this.handleError(); + log.info("Replace request on " + url.toString() + " returned Error HTTP status " + status); + ErrorHandler eh = new ErrorHandler(); + throw eh.handleError(resp); } - - // FIXME: we might want to error out here if we haven't already returned - return null; + else + { + throw new ProtocolViolationException("Unexpected response code " + status); + } } public SwordResponse addToMediaResource(String editMediaURL, Deposit deposit, AuthCredentials auth) - throws SWORDClientException, SWORDError + throws SWORDClientException, SWORDError, ProtocolViolationException { // some initial error checking and validation if (editMediaURL == null) @@ -630,13 +676,23 @@ // the response to a deposit on the Media Resource can be anything, although it is RECOMMENDED to be a // Deposit Receipt. Here we try to get a Deposit Receipt if possible - SwordResponse receipt = null; - int status = resp.getStatus(); - String location = http.getLocation(resp); - // if there was a success response... - if (status >= 200 && status < 300) - { - // we don't know what is returned, so we just sidestep the whole issue and give + int status = resp.getStatus(); + ResponseCodeManager rcm = new ResponseCodeManager(); + ResponseStatus rs = rcm.addToMediaResource(status); + if (rs.isCorrect() || rs.isIncorrectButAllowed()) + { + log.info("Delete request on " + url.toString() + " returned HTTP status " + status + "; SUCCESS"); + + if (rs.isIncorrectButAllowed()) + { + log.warn("Server responded with status " + status + " which is incorrect. Attempting to continue " + + "processing response ..."); + } + + SwordResponse receipt = null; + String location = http.getLocation(resp); + + // we don't know what is returned, so we just sidestep the whole issue and give // the client an inputstream try { @@ -649,29 +705,33 @@ { receipt = new BinaryResponse(status, location, is); } + + return receipt; } catch (IOException e) { throw new SWORDClientException(e); } - } - else if (status >= 400) + } + else if (rs.isIncorrectAndViolation()) { - // this is an error - // FIXME: this needs to handle all the other possible response codes - log.info("Deposit request on " + url.toString() + " return HTTP status " + status); - this.handleError(); + throw new ProtocolViolationException("Server responded with invalid status " + status); } + else if (rs.isError()) + { + // FIXME: this needs to handle all the other possible response codes + log.info("Replace request on " + url.toString() + " returned Error HTTP status " + status); + ErrorHandler eh = new ErrorHandler(); + throw eh.handleError(resp); + } else { - receipt = new SwordResponse(status, location); + throw new ProtocolViolationException("Unexpected response code " + status); } - - return receipt; } public DepositReceipt addToContainer(String editURL, Deposit deposit, AuthCredentials auth) - throws SWORDClientException, SWORDError + throws SWORDClientException, SWORDError, ProtocolViolationException { // some initial error checking and validation if (editURL == null) @@ -762,11 +822,19 @@ throw new SWORDClientException("Deposit Object does not have one/both of entry/content set"); } - int status = resp.getStatus(); - if (status == 200) + int status = resp.getStatus(); + ResponseCodeManager rcm = new ResponseCodeManager(); + ResponseStatus rs = rcm.addToContainer(status); + if (rs.isCorrect() || rs.isIncorrectButAllowed()) { log.info("Deposit request on " + url.toString() + " returned HTTP status " + status + "; SUCCESS"); + if (rs.isIncorrectButAllowed()) + { + log.warn("Server responded with status " + status + " which is incorrect. Attempting to continue " + + "processing response ..."); + } + // SUCCESS DepositReceipt receipt = null; Document<Entry> doc = resp.getDocument(); @@ -778,7 +846,7 @@ // if there is no doc, we need to see what the configuration asks us to do about getting it if (doc == null && this.config.returnDepositReceipt()) { - receipt = this.getDepositReceipt(location); + receipt = this.getDepositReceipt(location, auth); } else if (doc != null) { @@ -788,25 +856,127 @@ return receipt; // which may be null } - else + else if (rs.isIncorrectAndViolation()) + { + throw new ProtocolViolationException("Server responded with invalid status " + status); + } + else if (rs.isError()) { // FIXME: this needs to handle all the other possible response codes - log.info("Deposit request on " + url.toString() + " return HTTP status " + status); - this.handleError(); + log.info("Replace request on " + url.toString() + " returned Error HTTP status " + status); + ErrorHandler eh = new ErrorHandler(); + throw eh.handleError(resp); } + else + { + throw new ProtocolViolationException("Unexpected response code " + status); + } + } - // FIXME: we might want to error out here if we haven't already returned - return null; + public DepositReceipt getDepositReceipt(ClientResponse resp, AuthCredentials auth) + throws ProtocolViolationException, SWORDClientException, SWORDError + { + DepositReceipt receipt; + HttpHeaders http = new HttpHeaders(); + Document<Entry> doc = resp.getDocument(); + String location = http.getLocation(resp); + + // it is possible that the doc will be null + + // if there is no doc and no location header this is broken + if (doc == null && (location == null || "".equals(location))) + { + throw new ProtocolViolationException("SWORD Server responded " + resp.getStatus() + " but failed to provide a deposit receipt or a Location header"); + } + + + // if there is no doc, we need to see what the configuration asks us to do about getting it + if (doc == null && this.config.returnDepositReceipt()) + { + // load the deposit receipt from the location + receipt = this.getDepositReceipt(location, auth); + } + else if (doc == null) + { + receipt = new DepositReceipt(resp.getStatus(), location); + } + else + { + Entry entry = doc.getRoot(); + receipt = new DepositReceipt(resp.getStatus(), location, entry); + } + + return receipt; } - public DepositReceipt getDepositReceipt(String editURL) - throws SWORDClientException + public DepositReceipt getDepositReceipt(String editURL, AuthCredentials auth) + throws SWORDClientException, ProtocolViolationException, SWORDError { - return null; + // some initial error checking and validation + if (editURL == null) + { + log.error("Null URL passed into getDepositReceipt method"); + throw new SWORDClientException("Null URL passed into getDepositReceipt method"); + } + if (log.isDebugEnabled()) + { + log.debug("beginning retrieve on entry document/deposit receipt url " + editURL); + } + + AbderaClient client = new AbderaClient(this.abdera); + RequestOptions options = this.getDefaultRequestOptions(); + HttpHeaders http = new HttpHeaders(); + + // ensure that the URL is valid + URL url = this.formaliseURL(editURL); + if (log.isDebugEnabled()) + { + log.debug("Formalised Edit URL to " + url.toString()); + } + + // sort out the HTTP basic authentication credentials + this.prepAuth(auth, client, options); + + ClientResponse resp = client.get(url.toString(), options); + + int status = resp.getStatus(); + ResponseCodeManager rcm = new ResponseCodeManager(); + ResponseStatus rs = rcm.getDepositReceipt(status); + if (rs.isCorrect() || rs.isIncorrectButAllowed()) + { + log.info("Get request on " + url.toString() + " returned HTTP status " + status + "; SUCCESS"); + + if (rs.isIncorrectButAllowed()) + { + log.warn("Server responded with status " + status + " which is incorrect. Attempting to continue " + + "processing response ..."); + } + + // SUCCESS + Document<Entry> doc = resp.getDocument(); + Entry entry = doc.getRoot(); + DepositReceipt receipt = new DepositReceipt(resp.getStatus(), editURL, entry); + return receipt; + } + else if (rs.isIncorrectAndViolation()) + { + throw new ProtocolViolationException("Server responded with invalid status " + status); + } + else if (rs.isError()) + { + // FIXME: this needs to handle all the other possible response codes + log.info("Deposit request on " + url.toString() + " returned Error HTTP status " + status); + ErrorHandler eh = new ErrorHandler(); + throw eh.handleError(resp); + } + else + { + throw new ProtocolViolationException("Unexpected response code " + status); + } } public Statement getStatement(String statementUrl, Statement statementShell, AuthCredentials auth) - throws SWORDClientException, StatementParseException + throws SWORDClientException, StatementParseException, ProtocolViolationException, SWORDError { // some initial error checking and validation if (statementUrl == null) @@ -838,31 +1008,47 @@ ClientResponse resp = client.get(url.toString(), options); - // is this an atom feed or an oai-ore doc or something else int status = resp.getStatus(); - if (status == 200) + ResponseCodeManager rcm = new ResponseCodeManager(); + ResponseStatus rs = rcm.getStatement(status); + if (rs.isCorrect() || rs.isIncorrectButAllowed()) + { + log.info("Get request on " + url.toString() + " returned HTTP status " + status + "; SUCCESS"); + + if (rs.isIncorrectButAllowed()) + { + log.warn("Server responded with status " + status + " which is incorrect. Attempting to continue " + + "processing response ..."); + } + + statementShell.parse(resp); + return statementShell; + } + else if (rs.isIncorrectAndViolation()) { - statementShell.parse(resp); - return statementShell; + throw new ProtocolViolationException("Server responded with invalid status " + status); } - else + else if (rs.isError()) { // FIXME: this needs to handle all the other possible response codes - this.handleError(); + log.info("Deposit request on " + url.toString() + " returned Error HTTP status " + status); + ErrorHandler eh = new ErrorHandler(); + throw eh.handleError(resp); } - - // FIXME: we might want to error out here if we haven't already returned - return null; + else + { + throw new ProtocolViolationException("Unexpected response code " + status); + } } public Content getContent(String contentURL, String mimeType) - throws SWORDClientException + throws SWORDClientException, ProtocolViolationException, SWORDError { return this.getContent(contentURL, mimeType, UriRegistry.PACKAGE_SIMPLE_ZIP, null); } public Content getContent(String contentURL, String mimeType, String packaging, AuthCredentials auth) - throws SWORDClientException + throws SWORDClientException, ProtocolViolationException, SWORDError { // some initial error checking and validation if (contentURL == null) @@ -905,9 +1091,19 @@ // if the response is successful, get the service document out of the response, // and wrap it in the SWORD ServiceDocument class - if (resp.getType() == Response.ResponseType.SUCCESS) + int status = resp.getStatus(); + ResponseCodeManager rcm = new ResponseCodeManager(); + ResponseStatus rs = rcm.getContent(status); + if (rs.isCorrect() || rs.isIncorrectButAllowed()) { log.info("Successfully retrieved Content from " + url.toString()); + + if (rs.isIncorrectButAllowed()) + { + log.warn("Server responded with status " + status + " which is incorrect. Attempting to continue " + + "processing response ..."); + } + String responsePackaging = http.getPackaging(resp); MimeType responseType = resp.getContentType(); Content content = new Content(); @@ -954,17 +1150,21 @@ return content; } } - else if (resp.getStatus() == 406) + else if (rs.isIncorrectAndViolation()) + { + throw new ProtocolViolationException("Server responded with invalid status " + status); + } + else if (rs.isError()) { // FIXME: this needs to handle all the other possible response codes - log.info("Deposit request on " + url.toString() + " return HTTP status " + resp.getStatus()); - this.handleError(); + log.info("Deposit request on " + url.toString() + " returned Error HTTP status " + status); + ErrorHandler eh = new ErrorHandler(); + throw eh.handleError(resp); } - - // if we don't get anything respond with null - log.warn("Unable to retrieve content from " + url.toString() + "; responded with " + resp.getStatus() - + ". Possible problem with SWORD server, or URL"); - return null; + else + { + throw new ProtocolViolationException("Unexpected response code " + status); + } } private URL formaliseURL(String url) @@ -984,11 +1184,6 @@ } } - private void handleError() - { - - } - private void prepAuth(AuthCredentials auth, AbderaClient client, RequestOptions options) throws SWORDClientException { Modified: JavaClient2.0/src/main/java/org/swordapp/client/SWORDMultipartRequestEntity.java =================================================================== --- JavaClient2.0/src/main/java/org/swordapp/client/SWORDMultipartRequestEntity.java 2011-05-16 08:19:19 UTC (rev 320) +++ JavaClient2.0/src/main/java/org/swordapp/client/SWORDMultipartRequestEntity.java 2011-05-21 18:49:21 UTC (rev 321) @@ -90,6 +90,8 @@ // line break out.writeBytes("\r\n"); + // FIXME: this reads everything into memory, which is a terrible idea. This is from the Abdera code + // itself, so interesting to note that it's broken there too. Fix it. ByteArrayOutputStream output = new ByteArrayOutputStream(); while (this.input.read(buffer) != -1) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |