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.components.http;
018:
019: import org.apache.commons.httpclient.Header;
020: import org.apache.commons.httpclient.HttpMethod;
021: import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
022: import org.apache.commons.httpclient.methods.PostMethod;
023: import org.apache.commons.httpclient.methods.StringRequestEntity;
024: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
025: import org.apache.servicemix.jbi.jaxp.StringSource;
026:
027: import javax.jbi.messaging.MessageExchange;
028: import javax.jbi.messaging.NormalizedMessage;
029: import javax.xml.transform.Source;
030: import javax.xml.transform.TransformerException;
031: import javax.xml.transform.stream.StreamSource;
032:
033: import java.util.Iterator;
034:
035: /**
036: * A class which marshalls a client HTTP request to a NMS message
037: *
038: * @version $Revision: 426415 $
039: */
040: public class HttpClientMarshaler {
041:
042: protected SourceTransformer sourceTransformer;
043: private boolean streaming;
044: private String contentType = "text/xml";
045:
046: public HttpClientMarshaler() {
047: this (false);
048: }
049:
050: public HttpClientMarshaler(boolean streaming) {
051: this .sourceTransformer = new SourceTransformer();
052: this .streaming = streaming;
053: }
054:
055: /**
056: * @return the streaming
057: */
058: public boolean isStreaming() {
059: return streaming;
060: }
061:
062: /**
063: * @param streaming the streaming to set
064: */
065: public void setStreaming(boolean streaming) {
066: this .streaming = streaming;
067: }
068:
069: /**
070: * @return the contentType
071: */
072: public String getContentType() {
073: return contentType;
074: }
075:
076: /**
077: * @param contentType the contentType to set
078: */
079: public void setContentType(String contentType) {
080: this .contentType = contentType;
081: }
082:
083: public void toNMS(NormalizedMessage normalizedMessage,
084: HttpMethod method) throws Exception {
085: addNmsProperties(normalizedMessage, method);
086: if (streaming) {
087: normalizedMessage.setContent(new StreamSource(method
088: .getResponseBodyAsStream()));
089: } else {
090: normalizedMessage.setContent(new StringSource(method
091: .getResponseBodyAsString()));
092: }
093: }
094:
095: public void fromNMS(PostMethod method, MessageExchange exchange,
096: NormalizedMessage normalizedMessage) throws Exception,
097: TransformerException {
098: addHttpHeaders(method, normalizedMessage);
099: if (streaming) {
100: method.setContentChunked(true);
101: Source src = normalizedMessage.getContent();
102: if (src instanceof StreamSource
103: && ((StreamSource) src).getInputStream() != null) {
104: method.setRequestEntity(new InputStreamRequestEntity(
105: ((StreamSource) src).getInputStream(), -1));
106: } else {
107: String text = sourceTransformer
108: .toString(normalizedMessage.getContent());
109: method.setRequestEntity(new StringRequestEntity(text));
110:
111: }
112: } else {
113: String text = sourceTransformer.toString(normalizedMessage
114: .getContent());
115: method.setRequestEntity(new StringRequestEntity(text));
116: }
117: }
118:
119: protected void addHttpHeaders(HttpMethod method,
120: NormalizedMessage message) {
121: for (Iterator iter = message.getPropertyNames().iterator(); iter
122: .hasNext();) {
123: String name = (String) iter.next();
124: Object value = message.getProperty(name);
125: if (shouldIncludeHeader(message, name, value)) {
126: method.addRequestHeader(name, value.toString());
127: }
128: }
129: if (method.getRequestHeader("Content-Type") == null) {
130: method.setRequestHeader("Content-Type", contentType);
131: }
132: }
133:
134: protected void addNmsProperties(NormalizedMessage message,
135: HttpMethod method) {
136: Header[] headers = method.getResponseHeaders();
137: for (int i = 0; i < headers.length; i++) {
138: Header header = headers[i];
139: String name = header.getName();
140: String value = header.getValue();
141: message.setProperty(name, value);
142: }
143: }
144:
145: /**
146: * Decides whether or not the given header should be included in the JMS message.
147: * By default this includes all suitable typed values
148: */
149: protected boolean shouldIncludeHeader(
150: NormalizedMessage normalizedMessage, String name,
151: Object value) {
152: return value instanceof String
153: && !"Content-Length".equalsIgnoreCase(name)
154: && !"Content-Type".equalsIgnoreCase(name);
155: }
156:
157: }
|