From: Niels Erik G. Nielsen Date: Sat, 4 May 2013 02:30:19 +0000 (-0400) Subject: Rearranges pz2/sp client logic to isolate error messaging X-Git-Tag: v0.0.7~128 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=249aba186bb25b673bc701f4614cfe1981cd6767;p=mkjsf-moved-to-github.git Rearranges pz2/sp client logic to isolate error messaging Error message handling and generating consolidated and put in one place (for each of pz2 and sp). Is put in client.executeCommand, which no longer throws exceptions - all errors client errors sought captured here and converted to XML responses, thus simplifying things for the users of executeCommand. --- diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/CommandResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/CommandResponse.java index dc8d8c0..59d72ff 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/CommandResponse.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/CommandResponse.java @@ -4,4 +4,6 @@ public interface CommandResponse { public int getStatusCode(); public String getContentType(); public String getResponseString(); + public byte[] getBinaryResponse(); + public boolean isBinary(); } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/CommandThread.java b/src/main/java/com/indexdata/mkjsf/pazpar2/CommandThread.java index ce3df29..e3169b9 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/CommandThread.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/CommandThread.java @@ -1,21 +1,15 @@ package com.indexdata.mkjsf.pazpar2; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - import org.apache.log4j.Logger; -import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; -import com.indexdata.mkjsf.pazpar2.data.CommandError; public class CommandThread extends Thread { private static Logger logger = Logger.getLogger(CommandThread.class); Pazpar2Command command; SearchClient client; - private ByteArrayOutputStream baos = new ByteArrayOutputStream(); - private StringBuilder response = new StringBuilder(""); + CommandResponse commandResponse = null; public CommandThread (Pazpar2Command command, SearchClient client) { this.command = command; @@ -34,42 +28,21 @@ public class CommandThread extends Thread { */ public void run() { - if (command.getName().equals("search")) { + if (command.getCommandName().equals("search")) { client.setSearchCommand(command); } - try { - long start = System.currentTimeMillis(); - CommandResponse commandResponse = client.executeCommand(command, baos); - if (commandResponse.getStatusCode()==200) { - response.append(commandResponse.getResponseString()); - } else if (commandResponse.getStatusCode()==417) { - logger.error("Pazpar2 status code 417: " + baos.toString("UTF-8")); - response.append(CommandError.insertPazpar2ErrorXml(command.getName(), "Expectation failed (417)", commandResponse.getResponseString())); - } else { - String resp = baos.toString("UTF-8"); - logger.error("Pazpar2 status code was " + commandResponse.getStatusCode() + ": " + resp); - throw new Pazpar2ErrorException(resp,commandResponse.getStatusCode(),resp,null); - } - long end = System.currentTimeMillis(); - logger.debug("Executed " + command.getName() + " in " + (end-start) + " ms." ); - } catch (IOException e) { - response.append(CommandError.createErrorXml(command.getName(), "io", e.getMessage())); - logger.error(response.toString()); - } catch (Pazpar2ErrorException e) { - response.append(CommandError.createErrorXml(command.getName(), "pazpar2error", e.getMessage())); - logger.error(response.toString()); - } catch (Exception e) { - response.append(CommandError.createErrorXml(command.getName(), "general", e.getMessage())); - logger.error(response.toString()); - } + long start = System.currentTimeMillis(); + commandResponse = client.executeCommand(command); + long end = System.currentTimeMillis(); + logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." ); } /** * * @return Pazpar2 response as an XML string, possibly a generated error XML */ - public String getResponse () { - return response.toString(); + public CommandResponse getCommandResponse () { + return commandResponse; } public Pazpar2Command getCommand() { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java index ba2f0d7..f0cfa7d 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Bean.java @@ -132,10 +132,10 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable { } } for (CommandThread thread : threadList) { - String commandName = thread.getCommand().getName(); - String response = thread.getResponse(); - responseLogger.debug("Response was: " + response); - Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response); + String commandName = thread.getCommand().getCommandName(); + CommandResponse response = thread.getCommandResponse(); + responseLogger.debug("Response was: " + response.getResponseString()); + Pazpar2ResponseData responseObject = Pazpar2ResponseParser.getParser().getDataObject(response.getResponseString()); if (Pazpar2ResponseParser.docTypes.contains(responseObject.getType())) { pzresp.put(commandName, responseObject); } else { @@ -174,6 +174,9 @@ public class Pz2Bean implements Pz2Interface, StateListener, Serializable { } catch (NullPointerException npe) { npe.printStackTrace(); return ""; + } catch (Exception e) { + e.printStackTrace(); + return ""; } } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java index 78627d9..a68e67f 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2Client.java @@ -24,6 +24,7 @@ import com.indexdata.mkjsf.config.Configuration; import com.indexdata.mkjsf.config.ConfigurationReader; import com.indexdata.mkjsf.errors.ConfigurationException; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; +import com.indexdata.mkjsf.pazpar2.data.CommandError; import com.indexdata.mkjsf.utils.Utils; public class Pz2Client implements SearchClient { @@ -82,16 +83,43 @@ public class Pz2Client implements SearchClient { @Override public void setSearchCommand(Pazpar2Command command) { - ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString()); + ClientCommand clientCommand = new ClientCommand(command.getCommandName(), command.getEncodedQueryString()); client.setSearchCommand(clientCommand); } @Override - public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) - throws Pazpar2ErrorException, IOException { - ClientCommand clientCommand = new ClientCommand(command.getName(), command.getEncodedQueryString()); - Pazpar2HttpResponse pz2HttpResponse = client.executeCommand(clientCommand, baos); - return new Pz2CommandResponse(pz2HttpResponse,baos); + public CommandResponse executeCommand(Pazpar2Command command) { + Pz2CommandResponse commandResponse = null; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ClientCommand clientCommand = new ClientCommand(command.getCommandName(), command.getEncodedQueryString()); + Pazpar2HttpResponse pz2HttpResponse = null; + long start = System.currentTimeMillis(); + try { + pz2HttpResponse = client.executeCommand(clientCommand, baos); + if (pz2HttpResponse.getStatusCode()==200) { + commandResponse = new Pz2CommandResponse(pz2HttpResponse,baos); + } else if (pz2HttpResponse.getStatusCode()==417) { + logger.error("Pazpar2 status code 417: " + baos.toString("UTF-8")); + commandResponse = new Pz2CommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertPazpar2ErrorXml(command.getCommandName(), "Pazpar2: Expectation failed (417)", baos.toString("UTF-8")),"text/xml"); + } else { + String resp = baos.toString("UTF-8"); + logger.error("Pazpar2 status code was " + pz2HttpResponse.getStatusCode() + ": " + resp); + commandResponse = new Pz2CommandResponse(pz2HttpResponse.getStatusCode(),CommandError.insertPazpar2ErrorXml(command.getCommandName(), "Pazpar2 error occurred", baos.toString("UTF-8")),"text/xml"); + throw new Pazpar2ErrorException(resp,pz2HttpResponse.getStatusCode(),resp,null); + } + } catch (IOException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + commandResponse = new Pz2CommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), "io", e.getMessage()),"text/xml"); + } catch (Pazpar2ErrorException e) { + logger.error(e.getMessage()); + e.printStackTrace(); + logger.error("Creating error XML"); + commandResponse = new Pz2CommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), "io", e.getMessage()),"text/xml"); + } + long end = System.currentTimeMillis(); + logger.debug("Executed " + command.getCommandName() + " in " + (end-start) + " ms." ); + return commandResponse; } public Pz2Client cloneMe() { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2CommandResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2CommandResponse.java index 0471b66..df14da7 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2CommandResponse.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/Pz2CommandResponse.java @@ -8,32 +8,61 @@ import com.indexdata.masterkey.pazpar2.client.Pazpar2HttpResponse; public class Pz2CommandResponse implements CommandResponse { private Pazpar2HttpResponse pz2httpResponse = null; - private ByteArrayOutputStream content = null; + private int statusCode; + private String contentType; + private byte[] content = null; + private String contentString = null; public Pz2CommandResponse(Pazpar2HttpResponse pz2response, ByteArrayOutputStream content) { pz2httpResponse = pz2response; - this.content = content; + this.content = content.toByteArray(); + this.statusCode = pz2httpResponse.getStatusCode(); + this.contentType = pz2httpResponse.getContentType(); + } + + public Pz2CommandResponse(Pazpar2HttpResponse pz2response, String content) { + pz2httpResponse = pz2response; + this.contentString = content; + } + + public Pz2CommandResponse(int statusCode, String content, String contentType) { + this.statusCode = statusCode; + this.contentString = content; + this.contentType = contentType; } @Override public int getStatusCode() { - return pz2httpResponse.getStatusCode(); + return statusCode; } @Override public String getContentType() { - return pz2httpResponse.getContentType(); + return contentType; } @Override public String getResponseString() { - try { - return content.toString("UTF-8"); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - return null; + if (content == null) { + return contentString; + } else { + try { + return new String(content,"UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return "unsupported encoding"; + } } } + @Override + public byte[] getBinaryResponse() { + return content; + } + + @Override + public boolean isBinary() { + return !contentType.contains("xml"); + } } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/SearchClient.java b/src/main/java/com/indexdata/mkjsf/pazpar2/SearchClient.java index 1a67522..c7ec2c5 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/SearchClient.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/SearchClient.java @@ -1,10 +1,7 @@ package com.indexdata.mkjsf.pazpar2; -import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.Serializable; -import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; import com.indexdata.mkjsf.config.Configurable; import com.indexdata.mkjsf.config.Configuration; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; @@ -12,7 +9,7 @@ import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; public interface SearchClient extends Configurable, Serializable { public void setSearchCommand(Pazpar2Command command); - public CommandResponse executeCommand(Pazpar2Command command, ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException; + public CommandResponse executeCommand(Pazpar2Command command); // Use cloneMe() method if injecting the client with CDI. // The client is used for asynchronously sending off requests diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java index 8176512..73fd075 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/commands/Pazpar2Command.java @@ -25,7 +25,7 @@ public abstract class Pazpar2Command implements Serializable { public abstract Pazpar2Command copy (); - public String getName() { + public String getCommandName() { return name; } @@ -144,7 +144,7 @@ public abstract class Pazpar2Command implements Serializable { if (stateMgr != null) { stateMgr.checkIn(command); } else { - logger.info("Command '" + command.getName() + "' not affecting state (history) as no state manager was defined for this command."); + logger.info("Command '" + command.getCommandName() + "' not affecting state (history) as no state manager was defined for this command."); } } diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java b/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java index 5e07207..c8f9e2d 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/data/CommandError.java @@ -75,8 +75,8 @@ public class CommandError extends Pazpar2ResponseData implements ErrorInterface errorXml.append("<" + commandName + ">"+nl); errorXml.append(" "+nl); errorXml.append(" " + commandName + ""+nl); - errorXml.append(" " + XmlUtils.escape(exceptionName) + ""+nl); - errorXml.append(" " + XmlUtils.escape(errorMessage) + ""+nl); + errorXml.append(" " + (exceptionName != null ? XmlUtils.escape(exceptionName) : "") + ""+nl); + errorXml.append(" " + (errorMessage != null ? XmlUtils.escape(errorMessage) : "") + ""+nl); errorXml.append(" "+nl); errorXml.append(""+nl); return errorXml.toString(); diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java index 92e46a7..12cc734 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClient.java @@ -3,7 +3,6 @@ package com.indexdata.mkjsf.pazpar2.sp; import static com.indexdata.mkjsf.utils.Utils.nl; import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileReader; import java.io.IOException; @@ -13,6 +12,7 @@ import java.util.List; import java.util.Map; import java.util.StringTokenizer; +import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; @@ -32,7 +32,6 @@ import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.util.EntityUtils; import org.apache.log4j.Logger; -import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException; import com.indexdata.mkjsf.config.Configuration; import com.indexdata.mkjsf.config.ConfigurationReader; import com.indexdata.mkjsf.errors.ConfigurationException; @@ -41,6 +40,7 @@ import com.indexdata.mkjsf.pazpar2.SearchClient; import com.indexdata.mkjsf.pazpar2.commands.CommandParameter; import com.indexdata.mkjsf.pazpar2.commands.Pazpar2Command; import com.indexdata.mkjsf.pazpar2.commands.sp.AuthCommand; +import com.indexdata.mkjsf.pazpar2.data.CommandError; import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser; import com.indexdata.mkjsf.utils.Utils; @@ -83,6 +83,7 @@ public class ServiceProxyClient implements SearchClient { ipAuth = new AuthCommand(null); ipAuth.setParameterInState(new CommandParameter("action","=","ipauth")); } catch (ConfigurationException c) { + // TODO: c.printStackTrace(); } } @@ -99,77 +100,46 @@ public class ServiceProxyClient implements SearchClient { } public boolean authenticate (ServiceProxyUser user) { - try { - logger.info("Authenticating [" + user.getProperty("name") + "]"); - Pazpar2Command auth = new AuthCommand(null); - auth.setParametersInState(new CommandParameter("action","=","login"), - new CommandParameter("username","=",user.getProperty("name")), - new CommandParameter("password","=",user.getProperty("password"))); - byte[] response = send(auth); - String responseStr = new String(response,"UTF-8"); - logger.info(responseStr); - if (responseStr.contains("FAIL")) { - user.credentialsAuthenticationSucceeded(false); - return false; - } else { - user.credentialsAuthenticationSucceeded(true); - return true; - } - } catch (ClientProtocolException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + logger.info("Authenticating [" + user.getProperty("name") + "]"); + Pazpar2Command auth = new AuthCommand(null); + auth.setParametersInState(new CommandParameter("action","=","login"), + new CommandParameter("username","=",user.getProperty("name")), + new CommandParameter("password","=",user.getProperty("password"))); + ServiceProxyCommandResponse commandResponse = send(auth); + String responseStr = commandResponse.getResponseString(); + logger.info(responseStr); + if (responseStr.contains("FAIL")) { + user.credentialsAuthenticationSucceeded(false); return false; - } + } else { + user.credentialsAuthenticationSucceeded(true); + return true; + } } public boolean checkAuthentication (ServiceProxyUser user) { - try { - byte[] response = send(checkAuth); - logger.info(new String(response,"UTF-8")); - String responseStr = new String(response,"UTF-8"); - if (responseStr.contains("FAIL")) { - user.authenticationCheckFailed(); - return false; - } else { - return true; - } - } catch (ClientProtocolException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + ServiceProxyCommandResponse commandResponse = send(checkAuth); + String responseStr = commandResponse.getResponseString(); + logger.info(responseStr); + if (responseStr.contains("FAIL")) { + user.authenticationCheckFailed(); return false; - } + } else { + return true; + } } public boolean ipAuthenticate (ServiceProxyUser user) { - try { - byte[] response = send(ipAuth); - logger.info(new String(response,"UTF-8")); - String responseStr = new String(response,"UTF-8"); - if (responseStr.contains("FAIL")) { - user.ipAuthenticationSucceeded(false); - return false; - } else { - user.ipAuthenticationSucceeded(true); - return true; - } - } catch (ClientProtocolException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return false; - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + ServiceProxyCommandResponse commandResponse = send(ipAuth); + String responseStr = commandResponse.getResponseString(); + logger.info(responseStr); + if (responseStr.contains("FAIL")) { + user.ipAuthenticationSucceeded(false); return false; - } - + } else { + user.ipAuthenticationSucceeded(true); + return true; + } } public boolean isAuthenticatingClient () { @@ -191,24 +161,39 @@ public class ServiceProxyClient implements SearchClient { * @throws ClientProtocolException * @throws IOException */ - private byte[] send(Pazpar2Command command) throws ClientProtocolException, IOException { + private ServiceProxyCommandResponse send(Pazpar2Command command) { + ServiceProxyCommandResponse commandResponse = null; String url = selectedServiceUrl + "?" + command.getEncodedQueryString(); logger.info("Sending request "+url); HttpGet httpget = new HttpGet(url); - byte[] response = client.execute(httpget, handler); - return response; + byte[] response = null; + try { + response = client.execute(httpget, handler); + if (handler.getStatusCode()==200) { + commandResponse = new ServiceProxyCommandResponse(handler.getStatusCode(),response,handler.getContentType()); + } else { + logger.error("Service Proxy status code: " + handler.getStatusCode()); + commandResponse = new ServiceProxyCommandResponse(handler.getStatusCode(),CommandError.insertPazpar2ErrorXml(command.getCommandName(), "Service Proxy error occurred", new String(response,"UTF-8")),"text/xml"); + } + } catch (Exception e) { + e.printStackTrace(); + commandResponse = new ServiceProxyCommandResponse(-1,CommandError.createErrorXml(command.getCommandName(), e.getClass().getSimpleName(), (e.getMessage()!= null ? e.getMessage() : "") + (e.getCause()!=null ? e.getCause().getMessage() : "")),"text/xml"); + } + return commandResponse; } public class ProxyPz2ResponseHandler implements ResponseHandler { private StatusLine statusLine = null; + private Header contentType = null; public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException { byte[] resp = null; HttpEntity entity = response.getEntity(); statusLine = response.getStatusLine(); if (entity != null) { resp = EntityUtils.toByteArray(entity); - } - EntityUtils.consume(entity); + contentType = response.getEntity().getContentType(); + } + EntityUtils.consume(entity); return resp; } public int getStatusCode() { @@ -217,6 +202,9 @@ public class ServiceProxyClient implements SearchClient { public String getReasonPhrase() { return statusLine.getReasonPhrase(); } + public String getContentType () { + return (contentType != null ? contentType.getValue() : "Content-Type not known"); + } } public int getStatusCode () { @@ -233,11 +221,8 @@ public class ServiceProxyClient implements SearchClient { } @Override - public CommandResponse executeCommand(Pazpar2Command command, - ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException { - byte[] response = send(command); - baos.write(response); - return new ServiceProxyClientCommandResponse(getStatusCode(), new String(response,"UTF-8")); + public CommandResponse executeCommand(Pazpar2Command command) { + return send(command); } public ServiceProxyClient cloneMe() { diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClientCommandResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClientCommandResponse.java deleted file mode 100644 index 982233e..0000000 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyClientCommandResponse.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.indexdata.mkjsf.pazpar2.sp; - -import com.indexdata.mkjsf.pazpar2.CommandResponse; - -public class ServiceProxyClientCommandResponse implements CommandResponse { - - private int statusCode = 0; - private String content = null; - - public ServiceProxyClientCommandResponse(int statusCode, String content) { - this.statusCode = statusCode; - this.content = content; - } - - @Override - public int getStatusCode() { - return statusCode; - } - - @Override - public String getContentType() { - return "text/xml;charset=UTF-8"; - } - - @Override - public String getResponseString() { - return content; - } - -} diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyCommandResponse.java b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyCommandResponse.java new file mode 100644 index 0000000..6e8c683 --- /dev/null +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/sp/ServiceProxyCommandResponse.java @@ -0,0 +1,61 @@ +package com.indexdata.mkjsf.pazpar2.sp; + +import java.io.UnsupportedEncodingException; + +import com.indexdata.mkjsf.pazpar2.CommandResponse; + +public class ServiceProxyCommandResponse implements CommandResponse { + + private int statusCode = 0; + private byte[] content = null; + private String responseString = null; + private String contentType = ""; + + public ServiceProxyCommandResponse(int statusCode, byte[] content, String contentType) { + this.statusCode = statusCode; + this.content = content; + this.contentType = contentType; + } + + public ServiceProxyCommandResponse(int statusCode, String contentString, String contentType) { + this.statusCode = statusCode; + this.contentType = contentType; + this.responseString = contentString; + } + + @Override + public int getStatusCode() { + return statusCode; + } + + @Override + public String getContentType() { + return contentType; + } + + @Override + public String getResponseString() { + if (content == null) { + return responseString; + } else { + try { + return new String(content,"UTF-8"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return "unsupported encoding"; + } + } + } + + @Override + public byte[] getBinaryResponse() { + return content; + } + + @Override + public boolean isBinary() { + // TODO Auto-generated method stub + return false; + } + +} diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/state/Pazpar2State.java b/src/main/java/com/indexdata/mkjsf/pazpar2/state/Pazpar2State.java index e983d5c..dc2d2e1 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/state/Pazpar2State.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/state/Pazpar2State.java @@ -51,7 +51,7 @@ public class Pazpar2State { for (String commandName : previousState.commands.keySet()) { this.commands.put(commandName, previousState.commands.get(commandName).copy()); } - this.commands.put(newCommand.getName(),newCommand); + this.commands.put(newCommand.getCommandName(),newCommand); this.key = getKey(); } @@ -66,11 +66,11 @@ public class Pazpar2State { StringBuilder querystatebuilder = new StringBuilder(""); for (Pazpar2Command command : commands.values()) { if (command.hasParameters()) { - querystatebuilder.append("||"+command.getName()+"::"); + querystatebuilder.append("||"+command.getCommandName()+"::"); querystatebuilder.append(command.getValueWithExpressions()); } } - key = "#"+querystatebuilder.toString().hashCode(); + key = "#"+querystatebuilder.toString(); return key; } else { return key; @@ -86,9 +86,9 @@ public class Pazpar2State { public boolean stateMutating (Pazpar2Command command) { if (command == null) { return true; - } else if (commands.get(command.getName()) == null) { + } else if (commands.get(command.getCommandName()) == null) { return true; - } else if ((command.equals(commands.get(command.getName())))) { + } else if ((command.equals(commands.get(command.getCommandName())))) { return false; } else { return true; diff --git a/src/main/java/com/indexdata/mkjsf/pazpar2/state/StateManager.java b/src/main/java/com/indexdata/mkjsf/pazpar2/state/StateManager.java index f150dd8..c7661e0 100644 --- a/src/main/java/com/indexdata/mkjsf/pazpar2/state/StateManager.java +++ b/src/main/java/com/indexdata/mkjsf/pazpar2/state/StateManager.java @@ -64,15 +64,15 @@ public class StateManager implements Serializable { */ public void checkIn(Pazpar2Command command) { if (getCurrentState().stateMutating(command)) { - logger.debug("State changed by: " + command.getName()); + logger.debug("State changed by: " + command.getCommandName()); Pazpar2State state = new Pazpar2State(getCurrentState(),command); states.put(state.getKey(), state); currentKey = state.getKey(); - hasPendingStateChange(command.getName(),new Boolean(true)); + hasPendingStateChange(command.getCommandName(),new Boolean(true)); logger.debug("Updating " + listeners.size() + " listener(s) with state change from " + command); - updateListeners(command.getName()); + updateListeners(command.getCommandName()); } else { - logger.debug("Command " + command.getName() + " not found to change the state [" + command.getEncodedQueryString() + "]"); + logger.debug("Command " + command.getCommandName() + " not found to change the state [" + command.getEncodedQueryString() + "]"); } } @@ -96,7 +96,7 @@ public class StateManager implements Serializable { } else { logger.debug("State key change. Was: [" + currentKey + "]. Will be ["+key+"]"); if (states.get(key)==null) { - logger.error("The back-end received an unknow state key."); + logger.error("The back-end received an unknow state key: ["+ key +"]."); } else { if (states.get(key).getCommand("search").equals(states.get(currentKey).getCommand("search"))) { logger.debug("No search change detected");