KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > cxf > GeronimoDestination


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.cxf;
18
19 import java.io.IOException JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.lang.reflect.Field JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import org.apache.cxf.Bus;
29 import org.apache.cxf.message.Exchange;
30 import org.apache.cxf.message.Message;
31 import org.apache.cxf.message.MessageImpl;
32 import org.apache.cxf.service.model.EndpointInfo;
33 import org.apache.cxf.transport.Conduit;
34 import org.apache.cxf.transport.ConduitInitiator;
35 import org.apache.cxf.transport.Destination;
36 import org.apache.cxf.transport.MessageObserver;
37 import org.apache.cxf.transport.http.AbstractHTTPDestination;
38 import org.apache.cxf.ws.addressing.EndpointReferenceType;
39 import org.apache.cxf.wsdl.EndpointReferenceUtils;
40 import org.apache.geronimo.webservices.WebServiceContainer.Request;
41 import org.apache.geronimo.webservices.WebServiceContainer.Response;
42
43 public class GeronimoDestination extends AbstractHTTPDestination
44         implements Serializable JavaDoc {
45
46     private MessageObserver messageObserver;
47
48     public GeronimoDestination(Bus bus, ConduitInitiator conduitInitiator, EndpointInfo endpointInfo) throws IOException JavaDoc {
49         super(bus, conduitInitiator, endpointInfo);
50     }
51
52
53     public void invoke(Request request, Response response) {
54         Message message = new MessageImpl();
55         message.put(Request.class, request);
56         message.put(Response.class, response);
57         messageObserver.onMessage(message);
58     }
59
60     public Conduit getBackChannel(Message inMessage, Message partialResponse, EndpointReferenceType address) throws IOException JavaDoc {
61         Response response = inMessage.get(Response.class);
62         Conduit backChannel;
63         Exchange ex = inMessage.getExchange();
64         EndpointReferenceType target = address != null
65                 ? address
66                 : ex.get(EndpointReferenceType.class);
67         if (target == null) {
68             backChannel = new BackChannelConduit(response);
69         } else {
70             throw new IllegalArgumentException JavaDoc("RM not yet implemented");
71         }
72         return backChannel;
73     }
74
75     public void shutdown() {
76     }
77
78     @Override JavaDoc
79     protected void copyRequestHeaders(Message message, Map JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> headers) {
80         Request req = message.get(Request.class);
81
82         // no map of headers so just find all static field constants that begin with HEADER_, get
83
// its value and get the corresponding header.
84
for (Field JavaDoc field : Request.class.getFields()) {
85             if (field.getName().startsWith("HEADER_")) {
86                 try {
87                     assert field.getType().equals(String JavaDoc.class) : "unexpected field type";
88                     String JavaDoc headerName = (String JavaDoc) field.get(null);
89                     String JavaDoc headerValue = req.getHeader(headerName);
90                     if (headerValue != null) {
91                         List JavaDoc<String JavaDoc> values = headers.get(headerName);
92                         if (values == null) {
93                             values = new LinkedList JavaDoc<String JavaDoc>();
94                             headers.put(headerName, values);
95                         }
96                         values.addAll(splitMultipleHeaderValues(headerValue));
97                     }
98                 } catch (IllegalAccessException JavaDoc ex) {
99                     // ignore
100
}
101             }
102         }
103     }
104
105     private List JavaDoc<String JavaDoc> splitMultipleHeaderValues(String JavaDoc value) {
106
107         List JavaDoc<String JavaDoc> allValues = new LinkedList JavaDoc<String JavaDoc>();
108         if (value.contains(",")) {
109             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(value, ",");
110             while (st.hasMoreTokens()) {
111                 allValues.add(st.nextToken().trim());
112             }
113
114         } else {
115             allValues.add(value);
116         }
117         return allValues;
118     }
119
120
121     public void setMessageObserver(MessageObserver messageObserver) {
122         this.messageObserver = messageObserver;
123     }
124
125     protected class BackChannelConduit implements Conduit {
126
127         //TODO this will soon be publically available from somewhere in CXF
128
private static final String JavaDoc ANONYMOUS_ADDRESS =
129                 "http://www.w3.org/2005/08/addressing/anonymous";
130         protected Response response;
131         protected EndpointReferenceType target;
132
133         BackChannelConduit(Response resp) {
134             response = resp;
135             target = EndpointReferenceUtils.getEndpointReference(ANONYMOUS_ADDRESS);
136         }
137
138         public void close(Message msg) throws IOException JavaDoc {
139             msg.getContent(OutputStream JavaDoc.class).close();
140         }
141
142         /**
143          * Register a message observer for incoming messages.
144          *
145          * @param observer the observer to notify on receipt of incoming
146          */

147         public void setMessageObserver(MessageObserver observer) {
148             // shouldn't be called for a back channel conduit
149
}
150
151         /**
152          * Send an outbound message, assumed to contain all the name-value
153          * mappings of the corresponding input message (if any).
154          *
155          * @param message the message to be sent.
156          */

157         public void send(Message message) throws IOException JavaDoc {
158             message.put(Response.class, response);
159             //TODO gregw says this should work: current cxf-jetty code wraps output stream.
160
//if this doesn't work, we'd see an error from jetty saying you cant write headers to the output stream.
161
message.setContent(OutputStream JavaDoc.class, response.getOutputStream());
162         }
163
164         /**
165          * @return the reference associated with the target Destination
166          */

167         public EndpointReferenceType getTarget() {
168             return target;
169         }
170
171         /**
172          * Retreive the back-channel Destination.
173          *
174          * @return the backchannel Destination (or null if the backchannel is
175          * built-in)
176          */

177         public Destination getBackChannel() {
178             return null;
179         }
180
181         /**
182          * Close the conduit
183          */

184         public void close() {
185         }
186     }
187
188 }
189
Popular Tags