KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.jbi.JBIException;
20 import javax.jbi.messaging.ExchangeStatus;
21 import javax.jbi.messaging.InOnly;
22 import javax.jbi.messaging.InOut;
23 import javax.jbi.messaging.MessageExchangeFactory;
24 import javax.jbi.messaging.NormalizedMessage;
25 import javax.servlet.ServletException JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.xml.transform.TransformerException JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * A HTTP Binding Component which performs an {@link InOut} exchange with JBI and returns the response
33  * by default but is configurable to be an {@link InOnly} exchange.
34  *
35  * @version $Revision: 426415 $
36  */

37 public class HttpInOutBinding extends HttpBindingSupport {
38
39     private boolean defaultInOut = true;
40
41     public void process(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
42             JBIException {
43         //response.setContentType("application/soap+xml");
44
if (isInOutRequest(request, response)) {
45             processInOut(request, response);
46         }
47         else {
48             processInOnly(request, response);
49         }
50     }
51
52     public void processInOut(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
53             JBIException {
54         MessageExchangeFactory factory = getExchangeFactory();
55         InOut exchange = factory.createInOutExchange();
56         NormalizedMessage inMessage = exchange.createMessage();
57         try {
58             getMarshaler().toNMS(exchange, inMessage, request);
59             exchange.setInMessage(inMessage);
60             boolean result = getDeliveryChannel().sendSync(exchange);
61             if (result) {
62                 if (exchange.getStatus() == ExchangeStatus.ERROR) {
63                     if (exchange.getError() != null) {
64                         throw new ServletException JavaDoc(exchange.getError());
65                     } else {
66                         throw new ServletException JavaDoc("Exchange status is ERROR");
67                     }
68                 }
69                 getMarshaler().toResponse(exchange, exchange.getOutMessage(), response);
70             }
71             done(exchange);
72             response.setStatus(HttpServletResponse.SC_OK);
73         }
74         catch (IOException JavaDoc e) {
75             fail(exchange, e);
76             outputException(response, e);
77         }
78         catch (TransformerException JavaDoc e) {
79             fail(exchange, e);
80             outputException(response, e);
81         }
82     }
83
84     public void processInOnly(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc,
85             JBIException {
86         MessageExchangeFactory factory = getExchangeFactory();
87         InOnly exchange = factory.createInOnlyExchange();
88         NormalizedMessage inMessage = exchange.createMessage();
89         try {
90             HttpMarshaler marshaler = getMarshaler();
91             marshaler.toNMS(exchange, inMessage, request);
92             exchange.setInMessage(inMessage);
93             // Do a sendSync so that the stream is not closed
94
getDeliveryChannel().sendSync(exchange);
95             response.setStatus(HttpServletResponse.SC_OK);
96         }
97         catch (IOException JavaDoc e) {
98             fail(exchange, e);
99             outputException(response, e);
100         }
101     }
102
103     // Properties
104
//-------------------------------------------------------------------------
105
public boolean isDefaultInOut() {
106         return defaultInOut;
107     }
108
109     /**
110      * Sets whether an InOut (the default) or an InOnly message exchange will be used by default.
111      */

112     public void setDefaultInOut(boolean defaultInOut) {
113         this.defaultInOut = defaultInOut;
114     }
115
116     // Implementation methods
117
//-------------------------------------------------------------------------
118

119     /**
120      * Return true if this request is an {@link InOut} request otherwise it will be assumed to be an {@link InOnly}
121      */

122     protected boolean isInOutRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
123         return isDefaultInOut();
124     }
125
126
127 }
128
Popular Tags