001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.http.endpoints;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.Reader;
021: import java.net.URI;
022: import java.util.Map;
023:
024: import javax.jbi.messaging.ExchangeStatus;
025: import javax.jbi.messaging.Fault;
026: import javax.jbi.messaging.InOnly;
027: import javax.jbi.messaging.InOptionalOut;
028: import javax.jbi.messaging.InOut;
029: import javax.jbi.messaging.MessageExchange;
030: import javax.jbi.messaging.NormalizedMessage;
031: import javax.xml.transform.stream.StreamResult;
032: import javax.xml.transform.stream.StreamSource;
033:
034: import org.apache.commons.httpclient.HttpStatus;
035: import org.apache.servicemix.expression.Expression;
036: import org.apache.servicemix.http.jetty.SmxHttpExchange;
037: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
038: import org.apache.servicemix.jbi.jaxp.StAXSourceTransformer;
039: import org.mortbay.io.ByteArrayBuffer;
040: import org.mortbay.jetty.HttpHeaders;
041: import org.mortbay.jetty.HttpMethods;
042:
043: /**
044: * Default marshaler used for non-soap provider endpoints.
045: *
046: * @author gnodet
047: * @since 3.2
048: */
049: public class DefaultHttpProviderMarshaler implements
050: HttpProviderMarshaler {
051:
052: private SourceTransformer transformer = new StAXSourceTransformer();
053: private String locationURI;
054: private Expression locationURIExpression;
055: private String method;
056: private Expression methodExpression;
057: private String contentType = "text/xml";
058: private Expression contentTypeExpression;
059: private Map<String, String> headers;
060:
061: public String getLocationURI() {
062: return locationURI;
063: }
064:
065: public void setLocationURI(String locationUri) {
066: this .locationURI = locationUri;
067: }
068:
069: public Expression getLocationURIExpression() {
070: return locationURIExpression;
071: }
072:
073: public void setLocationURIExpression(
074: Expression locationUriExpression) {
075: this .locationURIExpression = locationUriExpression;
076: }
077:
078: public String getMethod() {
079: return method;
080: }
081:
082: public void setMethod(String method) {
083: this .method = method;
084: }
085:
086: public Expression getMethodExpression() {
087: return methodExpression;
088: }
089:
090: public void setMethodExpression(Expression methodExpression) {
091: this .methodExpression = methodExpression;
092: }
093:
094: public String getContentType() {
095: return contentType;
096: }
097:
098: public void setContentType(String contentType) {
099: this .contentType = contentType;
100: }
101:
102: public Expression getContentTypeExpression() {
103: return contentTypeExpression;
104: }
105:
106: public void setContentTypeExpression(
107: Expression contentTypeExpression) {
108: this .contentTypeExpression = contentTypeExpression;
109: }
110:
111: public Map<String, String> getHeaders() {
112: return headers;
113: }
114:
115: public void setHeaders(Map<String, String> headers) {
116: this .headers = headers;
117: }
118:
119: protected String getLocationUri(MessageExchange exchange,
120: NormalizedMessage inMsg) throws Exception {
121: String uri = null;
122: if (locationURIExpression != null) {
123: Object o = locationURIExpression.evaluate(exchange, inMsg);
124: uri = (o != null) ? o.toString() : null;
125: }
126: if (uri == null) {
127: uri = locationURI;
128: }
129: if (uri == null) {
130: throw new IllegalStateException(
131: "Unable to find URI for exchange");
132: }
133: return uri;
134: }
135:
136: protected String getMethod(MessageExchange exchange,
137: NormalizedMessage inMsg) throws Exception {
138: String mth = null;
139: if (methodExpression != null) {
140: Object o = methodExpression.evaluate(exchange, inMsg);
141: mth = (o != null) ? o.toString() : null;
142: }
143: if (mth == null) {
144: mth = method;
145: }
146: if (mth == null) {
147: if (inMsg.getContent() == null) {
148: mth = HttpMethods.GET;
149: } else {
150: mth = HttpMethods.POST;
151: }
152: }
153: return mth;
154: }
155:
156: protected String getContentType(MessageExchange exchange,
157: NormalizedMessage inMsg) throws Exception {
158: String content = null;
159: if (contentTypeExpression != null) {
160: Object o = contentTypeExpression.evaluate(exchange, inMsg);
161: content = (o != null) ? o.toString() : null;
162: }
163: if (content == null) {
164: content = contentType;
165: }
166: if (content == null) {
167: throw new IllegalStateException(
168: "ContentType must not be null");
169: }
170: return content;
171: }
172:
173: public void createRequest(final MessageExchange exchange,
174: final NormalizedMessage inMsg,
175: final SmxHttpExchange httpExchange) throws Exception {
176: httpExchange.setURL(getLocationUri(exchange, inMsg));
177:
178: // Temporary fix for bug in jetty-client 6.1.5
179: // http://fisheye.codehaus.org/browse/jetty-contrib/jetty/trunk/contrib/client/src/main/java/org/mortbay/jetty/client/HttpConnection.java?r1=374&r2=378
180: httpExchange.addRequestHeader(HttpHeaders.HOST_BUFFER,
181: new ByteArrayBuffer(new URI(getLocationUri(exchange,
182: inMsg)).getHost()));
183:
184: httpExchange.setMethod(getMethod(exchange, inMsg));
185: httpExchange.setRequestHeader(HttpHeaders.CONTENT_TYPE,
186: getContentType(exchange, inMsg));
187: if (getHeaders() != null) {
188: for (Map.Entry<String, String> e : getHeaders().entrySet()) {
189: httpExchange.setRequestHeader(e.getKey(), e.getValue());
190: }
191: }
192: if (inMsg.getContent() != null) {
193: ByteArrayOutputStream baos = new ByteArrayOutputStream();
194: transformer.toResult(inMsg.getContent(), new StreamResult(
195: baos));
196: httpExchange.setRequestContent(new ByteArrayBuffer(baos
197: .toByteArray()));
198: }
199: }
200:
201: public void handleResponse(MessageExchange exchange,
202: SmxHttpExchange httpExchange) throws Exception {
203: int response = httpExchange.getResponseStatus();
204: if (response != HttpStatus.SC_OK
205: && response != HttpStatus.SC_ACCEPTED) {
206: if (!(exchange instanceof InOnly)) {
207: Fault fault = exchange.createFault();
208: fault.setContent(new StreamSource(httpExchange
209: .getResponseReader()));
210: exchange.setFault(fault);
211: } else {
212: throw new Exception("Invalid status response: "
213: + response);
214: }
215: } else if (exchange instanceof InOut) {
216: NormalizedMessage msg = exchange.createMessage();
217: msg.setContent(new StreamSource(httpExchange
218: .getResponseReader()));
219: exchange.setMessage(msg, "out");
220: } else if (exchange instanceof InOptionalOut) {
221: Reader r = httpExchange.getResponseReader();
222: if (r != null) {
223: NormalizedMessage msg = exchange.createMessage();
224: msg.setContent(new StreamSource(r));
225: exchange.setMessage(msg, "out");
226: } else {
227: exchange.setStatus(ExchangeStatus.DONE);
228: }
229: } else {
230: exchange.setStatus(ExchangeStatus.DONE);
231:
232: }
233: }
234:
235: }
|