001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.webservice.jaxws;
020:
021: import java.io.IOException;
022: import java.io.Writer;
023:
024: import javax.servlet.ServletContext;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.log4j.Logger;
030:
031: import com.sun.xml.ws.transport.http.servlet.WSServlet;
032: import com.sun.xml.ws.transport.http.servlet.WSServletDelegate;
033:
034: import de.schlund.pfixcore.webservice.Constants;
035: import de.schlund.pfixcore.webservice.InsertPIResponseWrapper;
036: import de.schlund.pfixcore.webservice.ProcessingInfo;
037: import de.schlund.pfixcore.webservice.SOAPActionRequestWrapper;
038: import de.schlund.pfixcore.webservice.ServiceException;
039: import de.schlund.pfixcore.webservice.ServiceProcessor;
040: import de.schlund.pfixcore.webservice.ServiceRegistry;
041: import de.schlund.pfixcore.webservice.ServiceRequest;
042: import de.schlund.pfixcore.webservice.ServiceResponse;
043: import de.schlund.pfixcore.webservice.ServiceRuntime;
044:
045: /**
046: * @author mleidig@schlund.de
047: */
048: public class JAXWSProcessor implements ServiceProcessor {
049:
050: private static Logger LOG = Logger
051: .getLogger(ServiceProcessor.class);
052:
053: private ServletContext servletContext;
054: private WSServletDelegate delegate = null;
055:
056: private static ThreadLocal<JAXWSContext> currentJAXWSContext = new ThreadLocal<JAXWSContext>();
057:
058: public JAXWSProcessor(ServletContext servletContext) {
059: this .servletContext = servletContext;
060: delegate = (WSServletDelegate) servletContext
061: .getAttribute(WSServlet.JAXWS_RI_RUNTIME_INFO);
062: }
063:
064: public void process(ServiceRequest req, ServiceResponse res,
065: ServiceRuntime runtime, ServiceRegistry registry,
066: ProcessingInfo procInfo) throws ServiceException {
067:
068: if (!(req.getUnderlyingRequest() instanceof HttpServletRequest))
069: throw new ServiceException("Service protocol not supported");
070: HttpServletRequest httpReq = (HttpServletRequest) req
071: .getUnderlyingRequest();
072: HttpServletResponse httpRes = (HttpServletResponse) res
073: .getUnderlyingResponse();
074:
075: try {
076: JAXWSContext ctx = new JAXWSContext(procInfo);
077: setCurrentContext(ctx);
078:
079: if (httpReq.getHeader(Constants.HEADER_SOAP_ACTION) == null
080: && httpReq
081: .getParameter(Constants.PARAM_SOAP_MESSAGE) != null) {
082: if (LOG.isDebugEnabled())
083: LOG
084: .debug("no SOAPAction header, but soapmessage parameter -> iframe method");
085: String reqID = httpReq
086: .getParameter(Constants.PARAM_REQUEST_ID);
087: if (LOG.isDebugEnabled())
088: if (reqID != null)
089: LOG.debug("contains requestID parameter: "
090: + reqID);
091: String insPI = httpReq.getParameter("insertpi");
092: if (insPI != null)
093: httpRes = new InsertPIResponseWrapper(httpRes);
094: if (LOG.isDebugEnabled())
095: if (insPI != null)
096: LOG.debug("contains insertpi parameter");
097: httpReq = new SOAPActionRequestWrapper(httpReq);
098: } else if (httpReq.getHeader(Constants.HEADER_SOAP_ACTION) != null) {
099: if (LOG.isDebugEnabled())
100: LOG
101: .debug("found SOAPAction header, but no soapmessage parameter -> xmlhttprequest version");
102: String reqID = httpReq
103: .getHeader(Constants.HEADER_REQUEST_ID);
104: if (LOG.isDebugEnabled())
105: if (reqID != null)
106: LOG
107: .debug("contains requestID header: "
108: + reqID);
109: if (reqID != null)
110: httpRes.setHeader(Constants.HEADER_REQUEST_ID,
111: reqID);
112: }
113:
114: if (delegate != null) {
115: delegate.doPost(httpReq, httpRes, servletContext);
116: }
117: } catch (ServletException x) {
118: throw new ServiceException(
119: "Error processing webservice request", x);
120: } catch (IOException x) {
121: throw new ServiceException(
122: "Error processing webservice request", x);
123: } finally {
124: setCurrentContext(null);
125: }
126: }
127:
128: public void processException(ServiceRequest req,
129: ServiceResponse res, Exception exception)
130: throws ServiceException {
131: try {
132: if (res.getUnderlyingResponse() instanceof HttpServletResponse) {
133: ((HttpServletResponse) res.getUnderlyingResponse())
134: .setStatus(500);
135: }
136: res.setContentType("text/xml");
137: res.setCharacterEncoding("utf-8");
138: Writer out = res.getMessageWriter();
139: out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
140: out
141: .write("<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">");
142: out.write("<S:Body>");
143: out.write("<S:Fault>");
144: out.write("<faultcode>S:Server</faultcode>");
145: out.write("<faultstring>");
146: out.write(exception.getMessage());
147: out.write("</faultstring>");
148: out.write("<detail>");
149: out
150: .write("<ns:exception xmlns:ns=\"http://jax-ws.dev.java.net/\" class=\"");
151: out.write(exception.getClass().getName());
152: out.write("\"/>");
153: out.write("</detail>");
154: out.write("</S:Fault>");
155: out.write("</S:Body>");
156: out.write("</S:Envelope>");
157: } catch (IOException x) {
158: throw new ServiceException(
159: "IOException during service exception processing.",
160: x);
161: }
162: }
163:
164: private static void setCurrentContext(JAXWSContext context) {
165: currentJAXWSContext.set(context);
166: }
167:
168: protected static JAXWSContext getCurrentContext() {
169: return currentJAXWSContext.get();
170: }
171:
172: }
|