001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: StandardServlet.java 11798 2008-02-01 03:59:19Z lzheng $
023: */
024: package com.bostechcorp.cbesb.runtime.component.http.server;
025:
026: import java.io.IOException;
027:
028: import javax.jbi.component.ComponentContext;
029: import javax.jbi.messaging.ExchangeStatus;
030: import javax.jbi.messaging.Fault;
031: import javax.jbi.messaging.MessageExchange;
032: import javax.jbi.messaging.MessageExchangeFactory;
033: import javax.jbi.messaging.MessagingException;
034: import javax.jbi.messaging.NormalizedMessage;
035: import javax.jbi.servicedesc.ServiceEndpoint;
036: import javax.servlet.ServletException;
037: import javax.servlet.http.HttpServlet;
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpServletResponse;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043:
044: import com.bostechcorp.cbesb.common.constant.MetadataConstants;
045: import com.bostechcorp.cbesb.runtime.ccsl.lib.IHttpConsumerMarshaller;
046: import com.bostechcorp.cbesb.runtime.component.http.HttpEndpoint;
047: import com.bostechcorp.cbesb.runtime.component.util.wsdl.WsdlMepConstants;
048:
049: public class StandardServlet extends HttpServlet {
050:
051: private static final long serialVersionUID = -1418600096572748306L;
052:
053: protected final transient Log logger = LogFactory
054: .getLog(getClass());
055:
056: protected HttpEndpoint endpoint;
057: protected IHttpConsumerMarshaller marshaller;
058:
059: public StandardServlet(HttpEndpoint endpoint) {
060: this .endpoint = endpoint;
061: }
062:
063: protected IHttpConsumerMarshaller getMarshaller() {
064: if (marshaller == null) {
065: marshaller = endpoint.getHttpConsumerMarshaller();
066: }
067: return marshaller;
068: }
069:
070: /* (non-Javadoc)
071: * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
072: */
073: @Override
074: protected void service(HttpServletRequest request,
075: HttpServletResponse response) throws ServletException,
076: IOException {
077: long startTime = System.currentTimeMillis();
078:
079: try {
080: MessageExchange exchange = createMessageExchange();
081: NormalizedMessage inMsg = exchange.createMessage();
082: exchange.setMessage(inMsg, "in");
083: getMarshaller().servletRequestToNormalizedMessage(request,
084: inMsg, exchange, endpoint.getSvcDescHandler());
085: endpoint.getChannel().sendSync(exchange);
086:
087: if (endpoint.getDefaultMep()
088: .equals(WsdlMepConstants.IN_OUT)) {
089: // Maybe it had an error in called service
090: if ((ExchangeStatus.ERROR).equals(exchange.getStatus())) {
091: Fault fault = exchange.getFault();
092: getMarshaller().FaultToServletResponse(fault,
093: response, exchange);
094: } else {
095: NormalizedMessage outMsg = exchange
096: .getMessage("out");
097: if (outMsg != null) {
098: getMarshaller()
099: .NormalizedMessageToServletResponse(
100: outMsg, response, exchange);
101: } else {
102: throw new MessagingException(
103: "No out message returned for in-out exchange");
104: }
105: }
106:
107: exchange.setStatus(ExchangeStatus.DONE);
108: endpoint.getChannel().send(exchange);
109:
110: }
111:
112: this .endpoint.sendMessageProcessedNotification(System
113: .currentTimeMillis()
114: - startTime);
115:
116: } catch (Exception e) {
117: throw new ServletException(e);
118: }
119: }
120:
121: private MessageExchange createMessageExchange() throws Exception {
122: MessageExchange exchange = null;
123: MessageExchangeFactory meFactory = endpoint.getChannel()
124: .createExchangeFactory();
125: ComponentContext context = endpoint.getServiceUnit()
126: .getComponent().getComponentContext();
127: if (endpoint.getDefaultMep().equals(WsdlMepConstants.IN_ONLY)) {
128: exchange = meFactory.createInOnlyExchange();
129: } else if (endpoint.getDefaultMep().equals(
130: WsdlMepConstants.IN_OUT)) {
131: exchange = meFactory.createInOutExchange();
132: } else if (endpoint.getDefaultMep().equals(
133: WsdlMepConstants.ROBUST_IN_ONLY)) {
134: exchange = meFactory.createRobustInOnlyExchange();
135: } else {
136: throw new Exception("Unsupported MEP: "
137: + endpoint.getDefaultMep());
138: }
139: String endpointKey = "{"
140: + endpoint.getService().getNamespaceURI() + "}"
141: + endpoint.getService().getLocalPart() + ":"
142: + endpoint.getEndpoint();
143: exchange
144: .setProperty(
145: MetadataConstants.SENDER_ENDPOINT_PROPERTY,
146: endpointKey);
147:
148: logger.debug("Consumer endpoint service="
149: + endpoint.getService() + " endpoint="
150: + endpoint.getEndpoint());
151: ServiceEndpoint linkedEndpoint = context.getEndpoint(endpoint
152: .getService(), endpoint.getEndpoint());
153: logger.debug("Got target endpoint " + linkedEndpoint
154: + " service=" + linkedEndpoint.getServiceName()
155: + " endpoint=" + linkedEndpoint.getEndpointName());
156:
157: exchange.setEndpoint(linkedEndpoint);
158: exchange.setService(endpoint.getService());
159:
160: return exchange;
161: }
162:
163: }
|