1 package org.objectweb.celtix.bus.transports.http; 2 3 import java.io.BufferedOutputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.FilterOutputStream ; 6 import java.io.IOException ; 7 import java.io.OutputStream ; 8 import java.net.HttpURLConnection ; 9 import java.net.URL ; 10 import java.net.URLConnection ; 11 import java.util.HashMap ; 12 import java.util.List ; 13 import java.util.Map ; 14 15 import javax.xml.ws.handler.MessageContext; 16 17 import org.objectweb.celtix.bus.transports.http.HTTPClientTransport.HTTPClientInputStreamContext; 18 import org.objectweb.celtix.bus.ws.addressing.ContextUtils; 19 import org.objectweb.celtix.context.InputStreamMessageContext; 20 import org.objectweb.celtix.context.MessageContextWrapper; 21 import org.objectweb.celtix.context.OutputStreamMessageContext; 22 import org.objectweb.celtix.ws.addressing.EndpointReferenceType; 23 24 public abstract class AbstractHTTPRequestorOutputStreamContext 25 extends MessageContextWrapper 26 implements OutputStreamMessageContext { 27 28 URLConnection connection; 29 WrappedOutputStream origOut; 30 OutputStream out; 31 HTTPClientInputStreamContext inputStreamContext; 32 33 @SuppressWarnings ("unchecked") 34 public AbstractHTTPRequestorOutputStreamContext(MessageContext ctx) 35 throws IOException { 36 super(ctx); 37 38 Map <String , List <String >> headers = (Map <String , List <String >>)super.get(HTTP_REQUEST_HEADERS); 39 if (null == headers) { 40 headers = new HashMap <String , List <String >>(); 41 super.put(HTTP_REQUEST_HEADERS, headers); 42 } 43 44 EndpointReferenceType to = ContextUtils.retrieveTo(ctx); 45 if (to != null && to.getAddress() != null) { 46 URL url = new URL (to.getAddress().getValue()); 47 connection = getConnection(url); 48 connection.setDoOutput(true); 49 50 if (connection instanceof HttpURLConnection ) { 51 HttpURLConnection hc = (HttpURLConnection )connection; 52 hc.setRequestMethod("POST"); 53 } 54 } 55 origOut = new WrappedOutputStream(); 56 out = origOut; 57 } 58 59 protected abstract URLConnection getConnection(URL url) throws IOException ; 60 61 @SuppressWarnings ("unchecked") 62 void flushHeaders() throws IOException { 63 Map <String , List <String >> headers = (Map <String , List <String >>)super.get(HTTP_REQUEST_HEADERS); 64 if (null != headers) { 65 for (String header : headers.keySet()) { 66 List <String > headerList = headers.get(header); 67 for (String string : headerList) { 68 connection.addRequestProperty(header, string); 69 } 70 } 71 } 72 connection.addRequestProperty("Content-Type", "text/xml"); 73 origOut.resetOut(new BufferedOutputStream (connection.getOutputStream(), 1024)); 74 } 75 76 public void setFault(boolean isFault) { 77 } 79 80 public boolean isFault() { 81 return false; 82 } 83 84 public void setOneWay(boolean isOneWay) { 85 put(ONEWAY_MESSAGE_TF, isOneWay); 86 } 87 88 public boolean isOneWay() { 89 return ((Boolean )get(ONEWAY_MESSAGE_TF)).booleanValue(); 90 } 91 92 public OutputStream getOutputStream() { 93 return out; 94 } 95 96 public void setOutputStream(OutputStream o) { 97 out = o; 98 } 99 100 public InputStreamMessageContext getCorrespondingInputStreamContext() throws IOException { 101 if (inputStreamContext == null) { 102 inputStreamContext = new HTTPClientInputStreamContext(connection); 103 } 104 return inputStreamContext; 105 } 106 107 private class WrappedOutputStream extends FilterOutputStream { 108 WrappedOutputStream() { 109 super(new ByteArrayOutputStream ()); 110 } 111 void resetOut(OutputStream newOut) throws IOException { 112 ByteArrayOutputStream bout = (ByteArrayOutputStream )out; 113 if (bout.size() > 0) { 114 bout.writeTo(newOut); 115 } 116 out = newOut; 117 } 118 119 120 public void close() throws IOException { 121 out.flush(); 122 if (inputStreamContext != null) { 123 inputStreamContext.initialise(); 124 } 125 } 126 } 127 } 128 | Popular Tags |