KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > transports > http > AbstractHTTPServerTransport


1 package org.objectweb.celtix.bus.transports.http;
2
3 import java.io.IOException JavaDoc;
4 import java.net.URL JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.logging.Logger JavaDoc;
10
11 import javax.wsdl.Port;
12 import javax.wsdl.WSDLException;
13 import javax.xml.namespace.QName JavaDoc;
14 import javax.xml.ws.BindingProvider;
15 import javax.xml.ws.handler.MessageContext;
16
17 import org.objectweb.celtix.Bus;
18 import org.objectweb.celtix.bus.configuration.wsdl.WsdlHttpConfigurationProvider;
19 import org.objectweb.celtix.common.logging.LogUtils;
20 import org.objectweb.celtix.common.util.Base64Exception;
21 import org.objectweb.celtix.common.util.Base64Utility;
22 import org.objectweb.celtix.configuration.Configuration;
23 import org.objectweb.celtix.configuration.ConfigurationBuilder;
24 import org.objectweb.celtix.configuration.ConfigurationBuilderFactory;
25 import org.objectweb.celtix.context.GenericMessageContext;
26 import org.objectweb.celtix.context.ObjectMessageContext;
27 import org.objectweb.celtix.context.OutputStreamMessageContext;
28 import org.objectweb.celtix.transports.ServerTransport;
29 import org.objectweb.celtix.transports.ServerTransportCallback;
30 import org.objectweb.celtix.transports.http.configuration.HTTPServerPolicy;
31 import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
32 import org.objectweb.celtix.wsdl.EndpointReferenceUtils;
33
34 public abstract class AbstractHTTPServerTransport implements ServerTransport {
35     static final Logger JavaDoc LOG = LogUtils.getL7dLogger(AbstractHTTPServerTransport.class);
36     
37     private static final long serialVersionUID = 1L;
38     private static final String JavaDoc ENDPOINT_CONFIGURATION_URI =
39         "http://celtix.objectweb.org/bus/jaxws/endpoint-config";
40     private static final String JavaDoc HTTP_SERVER_CONFIGURATION_URI =
41         "http://celtix.objectweb.org/bus/transports/http/http-server-config";
42     private static final String JavaDoc HTTP_SERVER_CONFIGURATION_ID = "http-server";
43         
44
45     protected EndpointReferenceType reference;
46     protected String JavaDoc url;
47     protected String JavaDoc name;
48     protected URL JavaDoc nurl;
49     protected ServerTransportCallback callback;
50     protected Configuration configuration;
51     protected HTTPServerPolicy policy;
52     protected final Bus bus;
53     
54     public AbstractHTTPServerTransport(Bus b, EndpointReferenceType ref) throws WSDLException, IOException JavaDoc {
55         if (b == null) {
56             Thread.dumpStack();
57         }
58         reference = ref;
59         bus = b;
60         // get url (publish address) from endpoint reference
61
url = EndpointReferenceUtils.getAddress(ref);
62         configuration = createConfiguration(ref);
63         
64         nurl = new URL JavaDoc(url);
65         name = nurl.getPath();
66         policy = getServerPolicy(configuration);
67         
68        
69         
70     }
71     
72     private HTTPServerPolicy getServerPolicy(Configuration conf) {
73         HTTPServerPolicy pol = conf.getObject(HTTPServerPolicy.class, "httpServer");
74         if (pol == null) {
75             pol = new HTTPServerPolicy();
76         }
77         return pol;
78     }
79     
80     public OutputStreamMessageContext rebase(MessageContext context,
81                                              EndpointReferenceType decoupledResponseEndpoint)
82         throws IOException JavaDoc {
83         return null;
84     }
85
86     public void postDispatch(MessageContext bindingContext, OutputStreamMessageContext context) {
87         // Do not need to do anything here.
88
}
89     
90     public void shutdown() {
91     }
92     
93     private Configuration createConfiguration(EndpointReferenceType ref) {
94         Configuration busConfiguration = bus.getConfiguration();
95         QName JavaDoc serviceName = EndpointReferenceUtils.getServiceName(ref);
96         Configuration endpointConfiguration = busConfiguration
97             .getChild(ENDPOINT_CONFIGURATION_URI, serviceName.toString());
98         Port port = null;
99         try {
100             port = EndpointReferenceUtils.getPort(bus.getWSDLManager(), ref);
101         } catch (WSDLException ex) {
102             // ignore
103
}
104         ConfigurationBuilder cb = ConfigurationBuilderFactory.getBuilder(null);
105   
106         Configuration cfg = cb.getConfiguration(HTTP_SERVER_CONFIGURATION_URI,
107                                                 HTTP_SERVER_CONFIGURATION_ID,
108                                                 endpointConfiguration);
109         if (null == cfg) {
110             cfg = cb.buildConfiguration(HTTP_SERVER_CONFIGURATION_URI,
111                                         HTTP_SERVER_CONFIGURATION_ID,
112                                         endpointConfiguration);
113         }
114         // register the additional provider
115
if (null != port) {
116             cfg.getProviders().add(new WsdlHttpConfigurationProvider(port, true));
117         }
118         return cfg;
119     }
120
121     
122     protected void setHeaders(MessageContext ctx) {
123         ctx.put(ObjectMessageContext.MESSAGE_INPUT, true);
124         Map JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> headers = new HashMap JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>>();
125         copyRequestHeaders(ctx, headers);
126         ctx.put(GenericMessageContext.HTTP_REQUEST_HEADERS, headers);
127
128         
129         if (headers.containsKey("Authorization")) {
130             List JavaDoc<String JavaDoc> authorizationLines = headers.get("Authorization");
131             String JavaDoc credentials = authorizationLines.get(0);
132             String JavaDoc authType = credentials.split(" ")[0];
133             if ("Basic".equals(authType)) {
134                 String JavaDoc authEncoded = credentials.split(" ")[1];
135                 try {
136                     String JavaDoc authDecoded = new String JavaDoc(Base64Utility.decode(authEncoded));
137                     String JavaDoc authInfo[] = authDecoded.split(":");
138                     String JavaDoc username = authInfo[0];
139                     String JavaDoc password = authInfo[1];
140                     ctx.put(BindingProvider.USERNAME_PROPERTY, username);
141                     ctx.put(BindingProvider.PASSWORD_PROPERTY, password);
142                 } catch (Base64Exception ex) {
143                     //ignore, we'll leave things alone. They can try decoding it themselves
144
}
145             }
146         }
147         
148         headers = new HashMap JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>>();
149         setPolicies(ctx, headers);
150         ctx.put(GenericMessageContext.HTTP_RESPONSE_HEADERS, headers);
151     }
152     protected void setPolicies(MessageContext ctx, Map JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> headers) {
153         if (policy.isSetCacheControl()) {
154             headers.put("Cache-Control",
155                         Arrays.asList(new String JavaDoc[] {policy.getCacheControl().value()}));
156         }
157         if (policy.isSetContentLocation()) {
158             headers.put("Content-Location",
159                         Arrays.asList(new String JavaDoc[] {policy.getContentLocation()}));
160         }
161         if (policy.isSetContentEncoding()) {
162             headers.put("Content-Encoding",
163                         Arrays.asList(new String JavaDoc[] {policy.getContentEncoding()}));
164         }
165         if (policy.isSetContentType()) {
166             headers.put("Content-Type",
167                         Arrays.asList(new String JavaDoc[] {policy.getContentType()}));
168         }
169         if (policy.isSetServerType()) {
170             headers.put("Server",
171                         Arrays.asList(new String JavaDoc[] {policy.getServerType()}));
172         }
173         if (policy.isSetHonorKeepAlive() && !policy.isHonorKeepAlive()) {
174             headers.put("Connection",
175                         Arrays.asList(new String JavaDoc[] {"close"}));
176         }
177         
178     /*
179      * TODO - hook up these policies
180     <xs:attribute name="SuppressClientSendErrors" type="xs:boolean" use="optional" default="false">
181     <xs:attribute name="SuppressClientReceiveErrors" type="xs:boolean" use="optional" default="false">
182     */

183
184     }
185     
186     /**
187      * @param context The <code>OutputStreamMessageContext</code> to prepare.
188      */

189     public void finalPrepareOutputStreamContext(OutputStreamMessageContext context)
190         throws IOException JavaDoc {
191         if (context instanceof AbstractHTTPServerOutputStreamContext) {
192             ((AbstractHTTPServerOutputStreamContext)context).flushHeaders();
193         } else if (context instanceof AbstractHTTPRequestorOutputStreamContext) {
194             ((AbstractHTTPRequestorOutputStreamContext)context).flushHeaders();
195         }
196     }
197
198     protected abstract void copyRequestHeaders(MessageContext ctx, Map JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> headers);
199     
200
201 }
202
Popular Tags