001: package org.objectweb.celtix.bus.jaxws.servlet;
002:
003: import java.io.BufferedOutputStream;
004: import java.io.IOException;
005: import java.util.ArrayList;
006: import java.util.Enumeration;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Map;
010:
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013: import javax.wsdl.Definition;
014: import javax.wsdl.Port;
015: import javax.wsdl.WSDLException;
016: import javax.wsdl.extensions.ExtensibilityElement;
017: import javax.wsdl.extensions.soap.SOAPAddress;
018: import javax.xml.ws.handler.MessageContext;
019:
020: import org.objectweb.celtix.Bus;
021: import org.objectweb.celtix.bus.transports.http.AbstractHTTPServerOutputStreamContext;
022: import org.objectweb.celtix.bus.transports.http.AbstractHTTPServerTransport;
023: import org.objectweb.celtix.bus.transports.http.HTTPServerInputStreamContext;
024: import org.objectweb.celtix.context.OutputStreamMessageContext;
025: import org.objectweb.celtix.transports.ServerTransportCallback;
026: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
027: import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
028: import org.xmlsoap.schemas.wsdl.http.AddressType;
029:
030: public class ServletServerTransport extends AbstractHTTPServerTransport {
031:
032: CeltixServlet servlet;
033:
034: public ServletServerTransport(Bus b, EndpointReferenceType ref,
035: CeltixServlet s) throws WSDLException, IOException {
036: super (b, ref);
037: servlet = s;
038: }
039:
040: public void activate(ServerTransportCallback cb) throws IOException {
041: callback = cb;
042: servlet.addServant(nurl, this );
043: }
044:
045: public void deactivate() throws IOException {
046: servlet.removeServant(nurl, this );
047: }
048:
049: protected void copyRequestHeaders(MessageContext ctx,
050: Map<String, List<String>> headers) {
051: HttpServletRequest req = (HttpServletRequest) ctx
052: .get(HTTPServerInputStreamContext.HTTP_REQUEST);
053: for (Enumeration e = req.getHeaderNames(); e.hasMoreElements();) {
054: String fname = (String) e.nextElement();
055: List<String> values;
056: if (headers.containsKey(fname)) {
057: values = headers.get(fname);
058: } else {
059: values = new ArrayList<String>();
060: headers.put(fname, values);
061: }
062: for (Enumeration e2 = req.getHeaders(fname); e2
063: .hasMoreElements();) {
064: String val = (String) e2.nextElement();
065: values.add(val);
066: }
067: }
068: }
069:
070: protected void setPolicies(MessageContext ctx,
071: Map<String, List<String>> headers) {
072: super .setPolicies(ctx, headers);
073: if (policy.isSetReceiveTimeout()) {
074: /*
075: * @@TODO - can we set a timout?
076: Object connection = req.getHttpConnection().getConnection();
077: if (connection instanceof Socket) {
078: Socket sock = (Socket)connection;
079: try {
080: sock.setSoTimeout((int)policy.getReceiveTimeout());
081: } catch (SocketException ex) {
082: LOG.log(Level.INFO, "Could not set SoTimeout", ex);
083: }
084: }
085: */
086: }
087: }
088:
089: /**
090: * @param context The associated MessageContext.
091: * @return the context that will be used to obtain the OutputStream
092: */
093: public OutputStreamMessageContext createOutputStreamContext(
094: MessageContext context) throws IOException {
095: return new AbstractHTTPServerOutputStreamContext(this , context) {
096: protected void flushHeaders() throws IOException {
097: HttpServletResponse response = (HttpServletResponse) get(HTTPServerInputStreamContext.HTTP_RESPONSE);
098:
099: Integer i = (Integer) context.get(HTTP_RESPONSE_CODE);
100: if (i != null) {
101: response.setStatus(i.intValue());
102: }
103:
104: Map<?, ?> headers = (Map<?, ?>) super
105: .get(HTTP_RESPONSE_HEADERS);
106: if (null != headers) {
107: for (Iterator<?> iter = headers.keySet().iterator(); iter
108: .hasNext();) {
109: String header = (String) iter.next();
110: List<?> headerList = (List<?>) headers
111: .get(header);
112: for (Object string : headerList) {
113: response.addHeader(header, (String) string);
114: }
115: }
116: }
117: origOut.resetOut(new BufferedOutputStream(response
118: .getOutputStream(), 1024));
119: }
120: };
121: }
122:
123: public void doGet(HttpServletRequest req, HttpServletResponse resp)
124: throws IOException {
125: if (policy.isSetRedirectURL()) {
126: resp.sendRedirect(policy.getRedirectURL());
127: return;
128: }
129:
130: if ("GET".equals(req.getMethod())) {
131: try {
132: Definition def = EndpointReferenceUtils
133: .getWSDLDefinition(bus.getWSDLManager(),
134: reference);
135:
136: resp.addHeader("Content-Type", "text/xml");
137: Port port = EndpointReferenceUtils.getPort(bus
138: .getWSDLManager(), reference);
139: List<?> exts = port.getExtensibilityElements();
140: if (exts.size() > 0) {
141: ExtensibilityElement el = (ExtensibilityElement) exts
142: .get(0);
143: if (el instanceof SOAPAddress) {
144: SOAPAddress add = (SOAPAddress) el;
145: add.setLocationURI(req.getRequestURL()
146: .toString());
147: }
148: if (el instanceof AddressType) {
149: AddressType add = (AddressType) el;
150: add.setLocation(req.getRequestURL().toString());
151: }
152: }
153:
154: bus.getWSDLManager().getWSDLFactory().newWSDLWriter()
155: .writeWSDL(def, resp.getOutputStream());
156: resp.getOutputStream().flush();
157: return;
158: } catch (WSDLException ex) {
159: // TODO Auto-generated catch block
160: ex.printStackTrace();
161: }
162: }
163: }
164:
165: public void doPost(final HttpServletRequest req,
166: final HttpServletResponse resp) throws IOException {
167: HTTPServerInputStreamContext ctx = new HTTPServerInputStreamContext(
168: this ) {
169: public void initContext() throws IOException {
170: super.initContext();
171: inStream = req.getInputStream();
172: origInputStream = inStream;
173: }
174: };
175: ctx.put(HTTPServerInputStreamContext.HTTP_REQUEST, req);
176: ctx.put(HTTPServerInputStreamContext.HTTP_RESPONSE, resp);
177: ctx.initContext();
178:
179: callback.dispatch(ctx, this);
180: }
181: }
|