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 javax.jbi.JBIException;
020: import javax.jbi.messaging.ExchangeStatus;
021: import javax.jbi.messaging.InOnly;
022: import javax.jbi.messaging.InOut;
023: import javax.jbi.messaging.MessageExchangeFactory;
024: import javax.jbi.messaging.NormalizedMessage;
025: import javax.servlet.ServletException;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028: import javax.xml.transform.TransformerException;
029: import java.io.IOException;
030:
031: /**
032: * A HTTP Binding Component which performs an {@link InOut} exchange with JBI and returns the response
033: * by default but is configurable to be an {@link InOnly} exchange.
034: *
035: * @version $Revision: 426415 $
036: */
037: public class HttpInOutBinding extends HttpBindingSupport {
038:
039: private boolean defaultInOut = true;
040:
041: public void process(HttpServletRequest request,
042: HttpServletResponse response) throws ServletException,
043: IOException, JBIException {
044: //response.setContentType("application/soap+xml");
045: if (isInOutRequest(request, response)) {
046: processInOut(request, response);
047: } else {
048: processInOnly(request, response);
049: }
050: }
051:
052: public void processInOut(HttpServletRequest request,
053: HttpServletResponse response) throws ServletException,
054: IOException, JBIException {
055: MessageExchangeFactory factory = getExchangeFactory();
056: InOut exchange = factory.createInOutExchange();
057: NormalizedMessage inMessage = exchange.createMessage();
058: try {
059: getMarshaler().toNMS(exchange, inMessage, request);
060: exchange.setInMessage(inMessage);
061: boolean result = getDeliveryChannel().sendSync(exchange);
062: if (result) {
063: if (exchange.getStatus() == ExchangeStatus.ERROR) {
064: if (exchange.getError() != null) {
065: throw new ServletException(exchange.getError());
066: } else {
067: throw new ServletException(
068: "Exchange status is ERROR");
069: }
070: }
071: getMarshaler().toResponse(exchange,
072: exchange.getOutMessage(), response);
073: }
074: done(exchange);
075: response.setStatus(HttpServletResponse.SC_OK);
076: } catch (IOException e) {
077: fail(exchange, e);
078: outputException(response, e);
079: } catch (TransformerException e) {
080: fail(exchange, e);
081: outputException(response, e);
082: }
083: }
084:
085: public void processInOnly(HttpServletRequest request,
086: HttpServletResponse response) throws ServletException,
087: IOException, JBIException {
088: MessageExchangeFactory factory = getExchangeFactory();
089: InOnly exchange = factory.createInOnlyExchange();
090: NormalizedMessage inMessage = exchange.createMessage();
091: try {
092: HttpMarshaler marshaler = getMarshaler();
093: marshaler.toNMS(exchange, inMessage, request);
094: exchange.setInMessage(inMessage);
095: // Do a sendSync so that the stream is not closed
096: getDeliveryChannel().sendSync(exchange);
097: response.setStatus(HttpServletResponse.SC_OK);
098: } catch (IOException e) {
099: fail(exchange, e);
100: outputException(response, e);
101: }
102: }
103:
104: // Properties
105: //-------------------------------------------------------------------------
106: public boolean isDefaultInOut() {
107: return defaultInOut;
108: }
109:
110: /**
111: * Sets whether an InOut (the default) or an InOnly message exchange will be used by default.
112: */
113: public void setDefaultInOut(boolean defaultInOut) {
114: this .defaultInOut = defaultInOut;
115: }
116:
117: // Implementation methods
118: //-------------------------------------------------------------------------
119:
120: /**
121: * Return true if this request is an {@link InOut} request otherwise it will be assumed to be an {@link InOnly}
122: */
123: protected boolean isInOutRequest(HttpServletRequest request,
124: HttpServletResponse response) {
125: return isDefaultInOut();
126: }
127:
128: }
|