001: package org.objectweb.celtix.bus.transports.http;
002:
003: import java.io.BufferedOutputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.FilterOutputStream;
006: import java.io.IOException;
007: import java.io.OutputStream;
008: import java.net.HttpURLConnection;
009: import java.net.URL;
010: import java.net.URLConnection;
011: import java.util.HashMap;
012: import java.util.List;
013: import java.util.Map;
014:
015: import javax.xml.ws.handler.MessageContext;
016:
017: import org.objectweb.celtix.bus.transports.http.HTTPClientTransport.HTTPClientInputStreamContext;
018: import org.objectweb.celtix.bus.ws.addressing.ContextUtils;
019: import org.objectweb.celtix.context.InputStreamMessageContext;
020: import org.objectweb.celtix.context.MessageContextWrapper;
021: import org.objectweb.celtix.context.OutputStreamMessageContext;
022: import org.objectweb.celtix.ws.addressing.EndpointReferenceType;
023:
024: public abstract class AbstractHTTPRequestorOutputStreamContext extends
025: MessageContextWrapper implements OutputStreamMessageContext {
026:
027: URLConnection connection;
028: WrappedOutputStream origOut;
029: OutputStream out;
030: HTTPClientInputStreamContext inputStreamContext;
031:
032: @SuppressWarnings("unchecked")
033: public AbstractHTTPRequestorOutputStreamContext(MessageContext ctx)
034: throws IOException {
035: super (ctx);
036:
037: Map<String, List<String>> headers = (Map<String, List<String>>) super
038: .get(HTTP_REQUEST_HEADERS);
039: if (null == headers) {
040: headers = new HashMap<String, List<String>>();
041: super .put(HTTP_REQUEST_HEADERS, headers);
042: }
043:
044: EndpointReferenceType to = ContextUtils.retrieveTo(ctx);
045: if (to != null && to.getAddress() != null) {
046: URL url = new URL(to.getAddress().getValue());
047: connection = getConnection(url);
048: connection.setDoOutput(true);
049:
050: if (connection instanceof HttpURLConnection) {
051: HttpURLConnection hc = (HttpURLConnection) connection;
052: hc.setRequestMethod("POST");
053: }
054: }
055: origOut = new WrappedOutputStream();
056: out = origOut;
057: }
058:
059: protected abstract URLConnection getConnection(URL url)
060: throws IOException;
061:
062: @SuppressWarnings("unchecked")
063: void flushHeaders() throws IOException {
064: Map<String, List<String>> headers = (Map<String, List<String>>) super
065: .get(HTTP_REQUEST_HEADERS);
066: if (null != headers) {
067: for (String header : headers.keySet()) {
068: List<String> headerList = headers.get(header);
069: for (String string : headerList) {
070: connection.addRequestProperty(header, string);
071: }
072: }
073: }
074: connection.addRequestProperty("Content-Type", "text/xml");
075: origOut.resetOut(new BufferedOutputStream(connection
076: .getOutputStream(), 1024));
077: }
078:
079: public void setFault(boolean isFault) {
080: //nothing to do
081: }
082:
083: public boolean isFault() {
084: return false;
085: }
086:
087: public void setOneWay(boolean isOneWay) {
088: put(ONEWAY_MESSAGE_TF, isOneWay);
089: }
090:
091: public boolean isOneWay() {
092: return ((Boolean) get(ONEWAY_MESSAGE_TF)).booleanValue();
093: }
094:
095: public OutputStream getOutputStream() {
096: return out;
097: }
098:
099: public void setOutputStream(OutputStream o) {
100: out = o;
101: }
102:
103: public InputStreamMessageContext getCorrespondingInputStreamContext()
104: throws IOException {
105: if (inputStreamContext == null) {
106: inputStreamContext = new HTTPClientInputStreamContext(
107: connection);
108: }
109: return inputStreamContext;
110: }
111:
112: private class WrappedOutputStream extends FilterOutputStream {
113: WrappedOutputStream() {
114: super (new ByteArrayOutputStream());
115: }
116:
117: void resetOut(OutputStream newOut) throws IOException {
118: ByteArrayOutputStream bout = (ByteArrayOutputStream) out;
119: if (bout.size() > 0) {
120: bout.writeTo(newOut);
121: }
122: out = newOut;
123: }
124:
125: public void close() throws IOException {
126: out.flush();
127: if (inputStreamContext != null) {
128: inputStreamContext.initialise();
129: }
130: }
131: }
132: }
|