KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > http > HttpClientMarshaler


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.servicemix.components.http;
18
19 import org.apache.commons.httpclient.Header;
20 import org.apache.commons.httpclient.HttpMethod;
21 import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
22 import org.apache.commons.httpclient.methods.PostMethod;
23 import org.apache.commons.httpclient.methods.StringRequestEntity;
24 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
25 import org.apache.servicemix.jbi.jaxp.StringSource;
26
27 import javax.jbi.messaging.MessageExchange;
28 import javax.jbi.messaging.NormalizedMessage;
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.TransformerException JavaDoc;
31 import javax.xml.transform.stream.StreamSource JavaDoc;
32
33 import java.util.Iterator JavaDoc;
34
35 /**
36  * A class which marshalls a client HTTP request to a NMS message
37  *
38  * @version $Revision: 426415 $
39  */

40 public class HttpClientMarshaler {
41
42     protected SourceTransformer sourceTransformer;
43     private boolean streaming;
44     private String JavaDoc contentType = "text/xml";
45
46     public HttpClientMarshaler() {
47         this(false);
48     }
49     
50     public HttpClientMarshaler(boolean streaming) {
51         this.sourceTransformer = new SourceTransformer();
52         this.streaming = streaming;
53     }
54
55     /**
56      * @return the streaming
57      */

58     public boolean isStreaming() {
59         return streaming;
60     }
61
62     /**
63      * @param streaming the streaming to set
64      */

65     public void setStreaming(boolean streaming) {
66         this.streaming = streaming;
67     }
68
69     /**
70      * @return the contentType
71      */

72     public String JavaDoc getContentType() {
73         return contentType;
74     }
75
76     /**
77      * @param contentType the contentType to set
78      */

79     public void setContentType(String JavaDoc contentType) {
80         this.contentType = contentType;
81     }
82
83     public void toNMS(NormalizedMessage normalizedMessage, HttpMethod method) throws Exception JavaDoc {
84         addNmsProperties(normalizedMessage, method);
85         if (streaming) {
86             normalizedMessage.setContent(new StreamSource JavaDoc(method.getResponseBodyAsStream()));
87         } else {
88             normalizedMessage.setContent(new StringSource(method.getResponseBodyAsString()));
89         }
90     }
91
92     public void fromNMS(PostMethod method, MessageExchange exchange, NormalizedMessage normalizedMessage) throws Exception JavaDoc, TransformerException JavaDoc {
93         addHttpHeaders(method, normalizedMessage);
94         if (streaming) {
95             method.setContentChunked(true);
96             Source src = normalizedMessage.getContent();
97             if (src instanceof StreamSource JavaDoc && ((StreamSource JavaDoc) src).getInputStream() != null) {
98                 method.setRequestEntity(new InputStreamRequestEntity(
99                         ((StreamSource JavaDoc) src).getInputStream(), -1));
100             } else {
101                 String JavaDoc text = sourceTransformer.toString(normalizedMessage.getContent());
102                 method.setRequestEntity(new StringRequestEntity(text));
103                 
104             }
105         } else {
106             String JavaDoc text = sourceTransformer.toString(normalizedMessage.getContent());
107             method.setRequestEntity(new StringRequestEntity(text));
108         }
109     }
110
111     protected void addHttpHeaders(HttpMethod method, NormalizedMessage message) {
112         for (Iterator JavaDoc iter = message.getPropertyNames().iterator(); iter.hasNext();) {
113             String JavaDoc name = (String JavaDoc) iter.next();
114             Object JavaDoc value = message.getProperty(name);
115             if (shouldIncludeHeader(message, name, value)) {
116                 method.addRequestHeader(name, value.toString());
117             }
118         }
119         if (method.getRequestHeader("Content-Type") == null) {
120             method.setRequestHeader("Content-Type", contentType);
121         }
122     }
123
124     protected void addNmsProperties(NormalizedMessage message, HttpMethod method) {
125         Header[] headers = method.getResponseHeaders();
126         for (int i = 0; i < headers.length; i++) {
127             Header header = headers[i];
128             String JavaDoc name = header.getName();
129             String JavaDoc value = header.getValue();
130             message.setProperty(name, value);
131         }
132     }
133
134     /**
135      * Decides whether or not the given header should be included in the JMS message.
136      * By default this includes all suitable typed values
137      */

138     protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String JavaDoc name, Object JavaDoc value) {
139         return value instanceof String JavaDoc &&
140                 !"Content-Length".equalsIgnoreCase(name) &&
141                 !"Content-Type".equalsIgnoreCase(name);
142     }
143
144 }
145
Popular Tags