if(servletLog.isDebugEnabled()) {
servletLog.debug("request="+sb.toString());
servletLog.debug(Utilities.byteArrayToString(sb.toString().getBytes("UTF-8")));
}
msgContext.setProperty("sru", "");
AxisEngine engine=getEngine();
ByteArrayInputStream bais=new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
Message msg=new Message(bais, false);
msgContext.setRequestMessage(msg);
msgContext.setTargetService("SRW");
try {
engine.invoke(msgContext);
}
catch(Exception e) {
servletLog.error(sb.toString());
servletLog.error(e, e);
}
Message respMsg=msgContext.getResponseMessage();
if(respMsg!=null) {
javax.servlet.ServletOutputStream sos = null;
// code to strip SOAP stuff out. Hope this can go away some day
String soapResponse=respMsg.getSOAPPartAsString();
if("APP".equals(req.getAttribute("service"))||"APP".equals(req.getParameter("service"))) {
int start=soapResponse.indexOf("<recordData");
if(start>=0) {
String contentType=(String)req.getAttribute("ContentType");
if(contentType!=null)
resp.setContentType(contentType);
else
resp.setContentType("text/xml");
start=soapResponse.indexOf('>', start+1);
int stop=soapResponse.indexOf("</recordData>", start);
soapResponse=cleanup(soapResponse.substring(start+1, stop)
.toCharArray());
if(contentType!=null && contentType.toLowerCase().indexOf("html")>0) {
soapResponse=srwInfo.getXmlHeaders(req, db.searchStyleSheet)+Utilities.unXmlEncode(soapResponse);
}
if(negotiatedContent) {
String contentLocation=db.contentLocations.get(mediaType);
if(contentLocation!=null)
resp.setHeader("Content-Location", contentLocation);
negotiatedContent=false;
}
}
else { // this was an APP get, but we got nothing!
resp.setContentType("text/html");
sos=resp.getOutputStream();
sos.println("<html><head><title>Document not found</title></head><body><h2>404: Document not found</h2><hr/>OCLC SRW/SRU Server</body></html>");
sos.close();
resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
return;
}
}
else { // extract SRU response from inside the SRW response
int start=soapResponse.indexOf("<searchRetrieveResponse");
if(start>=0) {
int stop=soapResponse.indexOf("</searchRetrieveResponse>");
soapResponse="<searchRetrieveResponse xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.loc.gov/zing/srw/ http://www.loc.gov/standards/sru/sru1-1archive/xml-files/srw-types.xsd\""+cleanup(soapResponse.substring(start+23, stop+25)
.toCharArray());
if(negotiatedContent)
resp.setHeader("Content-Location", req.getRequestURI()+"?"+req.getQueryString()+"&httpAccept="+mediaType);
}
else { // damn, no searchRetrieveResponse!
resp.setContentType("text/html");
sos=resp.getOutputStream();
sos.println("<html><head><title>Document not found</title></head><body><h2>404: Document not found (No searchRetrieveResponse)</h2><hr/>OCLC SRW/SRU Server</body></html>");
sos.close();
resp.setStatus(HttpURLConnection.HTTP_NOT_FOUND);
return;
}
}
// now, let's see if they wanted something else
if(soapResponse.length()>0 && !mediaType.equals("")) {
Transformer trans=null;
trans=db.transformers.get(mediaType);
if(servletLog.isDebugEnabled())
servletLog.debug("trans="+trans);
if(trans!=null) {
StringWriter toRec = new StringWriter();
StreamSource fromRec = new StreamSource(new StringReader(soapResponse));
try {
trans.transform(fromRec, new StreamResult(toRec));
soapResponse=toRec.toString();
if(soapResponse.substring(0, Math.min(100, soapResponse.length())).contains("<?xml-stylesheet")) {
if(!mediaType.contains("html"))
resp.setContentType("text/xml");
}
else
resp.setContentType(mediaType);
} catch (TransformerException e) {
servletLog.error(e, e);
resp.setContentType("text/xml");
sos=resp.getOutputStream();
srwInfo.writeXmlHeader(sos, msgContext, req,
db.searchStyleSheet);
if(db.httpHeaderSetter!=null)
db.httpHeaderSetter.setGetResponseHeaders(db.searchRequest, db.response, soapResponse, req, resp);
}
}
else if(mediaType.contains("html")) {
// use the stylesheet that would have gone into the XML response as the transformer
String styleSheet=req.getParameter("stylesheet");
if(styleSheet==null)
styleSheet=db.searchStyleSheet;
String languages=req.getHeader("Accept-Language");
String realXsl=(String)SRWServletInfo.realXsls.get(languages+'/'+styleSheet);
if(servletLog.isDebugEnabled())
servletLog.debug("realXsl="+realXsl);
if(realXsl==null)
styleSheet=SRWServletInfo.findRealXsl(languages, req.getLocales(), styleSheet);
else
styleSheet=realXsl;
// if(!styleSheet.startsWith("/")) // relative URL
// styleSheet=req.getContextPath()+"/"+styleSheet;
if(servletLog.isDebugEnabled())
servletLog.debug("styleSheet="+styleSheet);
trans=transformers.get(styleSheet);
if(trans==null) { // gotta load it
// URL url=new URL("http://"+req.getServerName()+":"+req.getServerPort()+styleSheet);
URL url=new URL("http://localhost"+portNumber+styleSheet);
servletLog.info("stylesheet url: "+url);
HttpURLConnection conn=null;
try {
conn=(HttpURLConnection) url.openConnection();
servletLog.info("opened URL with response code "+conn.getResponseCode());
}
catch(ConnectException e) {
servletLog.error("ConnectException for stylesheet url: http://localhost"+portNumber+styleSheet);
}
if(conn.getResponseCode()!=200)
servletLog.error("response code="+conn.getResponseCode()+" getting "+url);
else {
StreamSource so=new StreamSource(conn.getInputStream());
so.setSystemId(url.toString());
try {
trans = TransformerFactory.newInstance().newTransformer(so);
transformers.put(mediaType, trans);
} catch (TransformerConfigurationException ex) {
servletLog.error("unable to build transformer "+url);
}
}
}
StringWriter toRec = new StringWriter();
StreamSource fromRec = new StreamSource(new StringReader(soapResponse));
try {
trans.transform(fromRec, new StreamResult(toRec));
soapResponse=toRec.toString();
if(soapResponse.substring(0, Math.min(100, soapResponse.length())).contains("<?xml-stylesheet")) {
resp.setContentType("text/xml");
}
else
resp.setContentType("text/html");
} catch (TransformerException e) {
servletLog.error(e, e);
resp.setContentType("text/xml");
sos=resp.getOutputStream();
srwInfo.writeXmlHeader(sos, msgContext, req,
db.searchStyleSheet);
if(db.httpHeaderSetter!=null)
db.httpHeaderSetter.setGetResponseHeaders(db.searchRequest, db.response, soapResponse, req, resp);
}
}
else {
String xmlHeaders=srwInfo.getXmlHeaders(req, db.searchStyleSheet).toString();
if(trans==null && xmlHeaders.substring(0, Math.min(100, xmlHeaders.length())).contains("xml-stylesheet"))
resp.setContentType("text/xml");
else if(soapResponse.substring(0, Math.min(100, soapResponse.length())).contains("xml-stylesheet"))
resp.setContentType("text/xml");
else
resp.setContentType(mediaType);
sos=resp.getOutputStream();
if(trans==null)
srwInfo.writeXmlHeader(sos, msgContext, req,
db.searchStyleSheet);
if(db.httpHeaderSetter!=null)
db.httpHeaderSetter.setGetResponseHeaders(db.searchRequest, db.response, soapResponse, req, resp);
}
}
else { //we don't care what they want, just return the SRU response
resp.setContentType("text/xml");
sos=resp.getOutputStream();
srwInfo.writeXmlHeader(sos, msgContext, req,
db.searchStyleSheet);
if(db.httpHeaderSetter!=null)
db.httpHeaderSetter.setGetResponseHeaders(db.searchRequest, db.response, soapResponse, req, resp);
}
//writer.println(soapResponse);
//writer.close();
if(sos==null)
sos=resp.getOutputStream();
if(db.httpHeaderSetter!=null)
db.httpHeaderSetter.setGetResponseHeaders(db.searchRequest, db.response, soapResponse, req, resp);
try {
sos.write(soapResponse.getBytes("utf-8"));
sos.close();
}
catch (IOException e) {
// no need to worry about this, let it slide
}
}
else {
resp.setContentType("text/html");
PrintWriter writer=resp.getWriter();
writer.println("<p>"+Messages.getMessage("noResponse01")+"</p>");
servletLog.error("request generated no response!");
writer.close();
}
// servletLog.info("at exit: totalMemory="+rt.totalMemory()+", freeMemory="+rt.freeMemory());
servletLog.debug("exit processMethodRequest");
return;
}
if((operation!=null && operation.equals("scan")) || scanClause!=null) { // scanRequest
int i;
String t;
StringBuilder sb=new StringBuilder();
sb.append("<soap:Envelope ")
.append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" ")
.append("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ")
.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">")
.append("<soap:Body>")
.append("<srw:scanRequest ")
.append("xmlns:srw=\"http://www.loc.gov/zing/srw/\">")
.append("<srw:scanClause>");
if(scanClause!=null) {
sb.append(encode(scanClause));
}
else
sb.append("\"\"");
sb.append("</srw:scanClause>");
t=req.getParameter("responsePosition");
if(t!=null) {
try {
i=Integer.parseInt(t);
if(i<0)
i=Integer.MAX_VALUE;
}
catch(NumberFormatException e){
i=Integer.MAX_VALUE;
}
sb.append("<srw:responsePosition>").append(i).append("</srw:responsePosition>");
}
t=req.getParameter("maximumTerms");
if(t!=null) {
try {
i=Integer.parseInt(t);
if(i<1)
i=Integer.MAX_VALUE;
}
catch(NumberFormatException e){
i=Integer.MAX_VALUE;
}
sb.append("<srw:maximumTerms>").append(i).append("</srw:maximumTerms>");
}
sb.append("</srw:scanRequest></soap:Body></soap:Envelope>");
if(servletLog.isDebugEnabled())
servletLog.debug(sb.toString());
msgContext.setProperty("sru", "");
msgContext.setTargetService("SRW");
AxisEngine engine=getEngine();
if(servletLog.isDebugEnabled())
servletLog.debug("request="+sb.toString());
ByteArrayInputStream bais=new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
Message msg=new Message(bais, false);
msgContext.setRequestMessage(msg);
try {
engine.invoke(msgContext);
}
catch(Exception e) {
servletLog.error(e, e);
}
Message respMsg=msgContext.getResponseMessage();