001: /*
002: * $Id: MuleRESTReceiverServlet.java 11343 2008-03-13 10:58:26Z tcarlson $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.transport.http.servlet;
012:
013: import org.mule.DefaultMuleMessage;
014: import org.mule.MuleServer;
015: import org.mule.RegistryContext;
016: import org.mule.api.MuleException;
017: import org.mule.api.MuleMessage;
018: import org.mule.api.endpoint.EndpointException;
019: import org.mule.api.endpoint.EndpointNotFoundException;
020: import org.mule.api.endpoint.InboundEndpoint;
021: import org.mule.api.transport.MessageReceiver;
022: import org.mule.transport.http.i18n.HttpMessages;
023:
024: import java.io.IOException;
025:
026: import javax.servlet.ServletException;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: /**
031: * <code>MuleRESTReceiverServlet</code> is used for sending a receiving events from
032: * the Mule server via a serlet container. The servlet uses the REST style of request
033: * processing GET METHOD will do a receive from an external source if an endpoint
034: * parameter is set otherwise it behaves the same way as POST. you can either specify
035: * the transport name i.e. to read from Jms orders.queue
036: * http://www.mycompany.com/rest/jms/orders/queue <p/> or a Mule endpoint name to
037: * target a specific endpoint config. This would get the first email message received
038: * by the orderEmailInbox endpoint. <p/>
039: * http://www.mycompany.com/rest/ordersEmailInbox <p/> POST Do a sysnchrous call and
040: * return a result http://www.clientapplication.com/service/clientquery?custId=1234
041: * <p/> PUT Do an asysnchrous call without returning a result (other than an http
042: * status code) http://www.clientapplication.com/service/orders?payload=<order>more
043: * beer</order> <p/> DELETE Same as GET only without returning a result
044: */
045:
046: public class MuleRESTReceiverServlet extends MuleReceiverServlet {
047: /**
048: * Serial version
049: */
050: private static final long serialVersionUID = -2395763805839859649L;
051:
052: protected void doGet(HttpServletRequest httpServletRequest,
053: HttpServletResponse httpServletResponse)
054: throws ServletException, IOException {
055: try {
056: if (httpServletRequest.getParameter("endpoint") != null) {
057: InboundEndpoint endpoint = getEndpointForURI(httpServletRequest);
058: String timeoutString = httpServletRequest
059: .getParameter("timeout");
060: long to = timeout;
061:
062: if (timeoutString != null) {
063: to = Long.parseLong(timeoutString);
064: }
065:
066: if (logger.isDebugEnabled()) {
067: logger.debug("Making request using endpoint: "
068: + endpoint.toString() + " timeout is: "
069: + to);
070: }
071:
072: MuleMessage returnMessage = endpoint.request(to);
073: writeResponse(httpServletResponse, returnMessage);
074: } else {
075: MessageReceiver receiver = getReceiverForURI(httpServletRequest);
076: httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME,
077: payloadParameterName);
078: MuleMessage message = new DefaultMuleMessage(receiver
079: .getConnector().getMessageAdapter(
080: httpServletRequest));
081: MuleMessage returnMessage = receiver.routeMessage(
082: message, true);
083: writeResponse(httpServletResponse, returnMessage);
084: }
085: } catch (Exception e) {
086: handleException(e,
087: "Failed to route event through Servlet Receiver",
088: httpServletResponse);
089: }
090: }
091:
092: protected void doPost(HttpServletRequest httpServletRequest,
093: HttpServletResponse httpServletResponse)
094: throws ServletException, IOException {
095: try {
096: MessageReceiver receiver = getReceiverForURI(httpServletRequest);
097: httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME,
098: payloadParameterName);
099: MuleMessage message = new DefaultMuleMessage(receiver
100: .getConnector().getMessageAdapter(
101: httpServletRequest));
102: MuleMessage returnMessage = receiver.routeMessage(message,
103: true);
104: writeResponse(httpServletResponse, returnMessage);
105:
106: } catch (Exception e) {
107: handleException(e, "Failed to Post event to Mule",
108: httpServletResponse);
109: }
110: }
111:
112: protected void doPut(HttpServletRequest httpServletRequest,
113: HttpServletResponse httpServletResponse)
114: throws ServletException, IOException {
115: try {
116: MessageReceiver receiver = getReceiverForURI(httpServletRequest);
117: httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME,
118: payloadParameterName);
119: MuleMessage message = new DefaultMuleMessage(receiver
120: .getConnector().getMessageAdapter(
121: httpServletRequest));
122: receiver
123: .routeMessage(message, MuleServer.getMuleContext()
124: .getConfiguration()
125: .isDefaultSynchronousEndpoints());
126:
127: httpServletResponse
128: .setStatus(HttpServletResponse.SC_CREATED);
129: if (feedback) {
130: httpServletResponse.getWriter().write(
131: "Item was created at endpointUri: "
132: + receiver.getEndpointURI());
133: }
134: } catch (Exception e) {
135: handleException(e, "Failed to Post event to Mule"
136: + e.getMessage(), httpServletResponse);
137: }
138: }
139:
140: protected void doDelete(HttpServletRequest httpServletRequest,
141: HttpServletResponse httpServletResponse)
142: throws ServletException, IOException {
143: try {
144: InboundEndpoint endpoint = getEndpointForURI(httpServletRequest);
145: String timeoutString = httpServletRequest
146: .getParameter("timeout");
147: long to = timeout;
148:
149: if (timeoutString != null) {
150: to = new Long(timeoutString).longValue();
151: }
152:
153: if (logger.isDebugEnabled()) {
154: logger.debug("Making request using endpoint: "
155: + endpoint.toString() + " timeout is: " + to);
156: }
157:
158: MuleMessage returnMessage = endpoint.request(to);
159: if (returnMessage != null) {
160: httpServletResponse
161: .setStatus(HttpServletResponse.SC_OK);
162: } else {
163: httpServletResponse
164: .setStatus(HttpServletResponse.SC_NO_CONTENT);
165: }
166: } catch (Exception e) {
167: handleException(e,
168: "Failed to Delete mule event via receive using uri: "
169: + httpServletRequest.getPathInfo(),
170: httpServletResponse);
171: }
172: }
173:
174: protected InboundEndpoint getEndpointForURI(
175: HttpServletRequest httpServletRequest) throws MuleException {
176: String endpointName = httpServletRequest
177: .getParameter("endpoint");
178: if (endpointName == null) {
179: throw new EndpointException(HttpMessages
180: .httpParameterNotSet("endpoint"));
181: }
182:
183: InboundEndpoint endpoint = RegistryContext.getRegistry()
184: .lookupEndpointFactory().getInboundEndpoint(
185: endpointName);
186: if (endpoint == null) {
187: // if we dont find an endpoint for the given name, lets check the
188: // servlet receivers
189: MessageReceiver receiver = (MessageReceiver) getReceivers()
190: .get(endpointName);
191:
192: if (receiver == null) {
193: throw new EndpointNotFoundException(endpointName);
194: }
195:
196: endpoint = receiver.getEndpoint();
197:
198: }
199: return endpoint;
200: }
201: }
|