1 package org.objectweb.celtix.bus.transports.http; 2 3 import java.io.IOException ; 4 import java.net.URL ; 5 import java.util.Arrays ; 6 import java.util.HashMap ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.logging.Logger ; 10 11 import javax.wsdl.Port; 12 import javax.wsdl.WSDLException; 13 import javax.xml.namespace.QName ; 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 LOG = LogUtils.getL7dLogger(AbstractHTTPServerTransport.class); 36 37 private static final long serialVersionUID = 1L; 38 private static final String ENDPOINT_CONFIGURATION_URI = 39 "http://celtix.objectweb.org/bus/jaxws/endpoint-config"; 40 private static final String HTTP_SERVER_CONFIGURATION_URI = 41 "http://celtix.objectweb.org/bus/transports/http/http-server-config"; 42 private static final String HTTP_SERVER_CONFIGURATION_ID = "http-server"; 43 44 45 protected EndpointReferenceType reference; 46 protected String url; 47 protected String name; 48 protected URL 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 { 55 if (b == null) { 56 Thread.dumpStack(); 57 } 58 reference = ref; 59 bus = b; 60 url = EndpointReferenceUtils.getAddress(ref); 62 configuration = createConfiguration(ref); 63 64 nurl = new URL (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 { 83 return null; 84 } 85 86 public void postDispatch(MessageContext bindingContext, OutputStreamMessageContext context) { 87 } 89 90 public void shutdown() { 91 } 92 93 private Configuration createConfiguration(EndpointReferenceType ref) { 94 Configuration busConfiguration = bus.getConfiguration(); 95 QName 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 } 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 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 <String , List <String >> headers = new HashMap <String , List <String >>(); 125 copyRequestHeaders(ctx, headers); 126 ctx.put(GenericMessageContext.HTTP_REQUEST_HEADERS, headers); 127 128 129 if (headers.containsKey("Authorization")) { 130 List <String > authorizationLines = headers.get("Authorization"); 131 String credentials = authorizationLines.get(0); 132 String authType = credentials.split(" ")[0]; 133 if ("Basic".equals(authType)) { 134 String authEncoded = credentials.split(" ")[1]; 135 try { 136 String authDecoded = new String (Base64Utility.decode(authEncoded)); 137 String authInfo[] = authDecoded.split(":"); 138 String username = authInfo[0]; 139 String password = authInfo[1]; 140 ctx.put(BindingProvider.USERNAME_PROPERTY, username); 141 ctx.put(BindingProvider.PASSWORD_PROPERTY, password); 142 } catch (Base64Exception ex) { 143 } 145 } 146 } 147 148 headers = new HashMap <String , List <String >>(); 149 setPolicies(ctx, headers); 150 ctx.put(GenericMessageContext.HTTP_RESPONSE_HEADERS, headers); 151 } 152 protected void setPolicies(MessageContext ctx, Map <String , List <String >> headers) { 153 if (policy.isSetCacheControl()) { 154 headers.put("Cache-Control", 155 Arrays.asList(new String [] {policy.getCacheControl().value()})); 156 } 157 if (policy.isSetContentLocation()) { 158 headers.put("Content-Location", 159 Arrays.asList(new String [] {policy.getContentLocation()})); 160 } 161 if (policy.isSetContentEncoding()) { 162 headers.put("Content-Encoding", 163 Arrays.asList(new String [] {policy.getContentEncoding()})); 164 } 165 if (policy.isSetContentType()) { 166 headers.put("Content-Type", 167 Arrays.asList(new String [] {policy.getContentType()})); 168 } 169 if (policy.isSetServerType()) { 170 headers.put("Server", 171 Arrays.asList(new String [] {policy.getServerType()})); 172 } 173 if (policy.isSetHonorKeepAlive() && !policy.isHonorKeepAlive()) { 174 headers.put("Connection", 175 Arrays.asList(new String [] {"close"})); 176 } 177 178 183 184 } 185 186 189 public void finalPrepareOutputStreamContext(OutputStreamMessageContext context) 190 throws IOException { 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 <String , List <String >> headers); 199 200 201 } 202 | Popular Tags |