1 10 11 package org.mule.providers.http.servlet; 12 13 import org.mule.MuleManager; 14 import org.mule.config.i18n.Message; 15 import org.mule.impl.MuleMessage; 16 import org.mule.providers.AbstractMessageReceiver; 17 import org.mule.umo.UMOMessage; 18 import org.mule.umo.endpoint.EndpointException; 19 import org.mule.umo.endpoint.EndpointNotFoundException; 20 import org.mule.umo.endpoint.MalformedEndpointException; 21 import org.mule.umo.endpoint.UMOEndpoint; 22 import org.mule.umo.provider.UMOMessageReceiver; 23 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 28 import java.io.IOException ; 29 30 48 49 public class MuleRESTReceiverServlet extends MuleReceiverServlet 50 { 51 54 private static final long serialVersionUID = -2395763805839859649L; 55 56 protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 57 throws ServletException , IOException 58 { 59 try 60 { 61 if (httpServletRequest.getParameter("endpoint") != null) 62 { 63 UMOEndpoint endpoint = getEndpointForURI(httpServletRequest); 64 String timeoutString = httpServletRequest.getParameter("timeout"); 65 long to = timeout; 66 if (timeoutString != null) 67 { 68 to = Long.parseLong(timeoutString); 69 } 70 if (logger.isDebugEnabled()) 71 { 72 logger.debug("Making request using endpoint: " + endpoint.toString() + " timeout is: " 73 + to); 74 } 75 76 UMOMessage returnMessage = endpoint.getConnector().getDispatcher(endpoint).receive(endpoint, 77 to); 78 79 writeResponse(httpServletResponse, returnMessage); 80 } 81 else 82 { 83 AbstractMessageReceiver receiver = getReceiverForURI(httpServletRequest); 84 httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName); 85 UMOMessage message = new MuleMessage(receiver.getConnector().getMessageAdapter( 86 httpServletRequest)); 87 UMOMessage returnMessage = receiver.routeMessage(message, true); 88 writeResponse(httpServletResponse, returnMessage); 89 } 90 } 91 catch (Exception e) 92 { 93 handleException(e, "Failed to route event through Servlet Receiver", httpServletResponse); 94 } 95 } 96 97 protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 98 throws ServletException , IOException 99 { 100 try 101 { 102 AbstractMessageReceiver receiver = getReceiverForURI(httpServletRequest); 103 httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName); 104 UMOMessage message = new MuleMessage(receiver.getConnector() 105 .getMessageAdapter(httpServletRequest)); 106 UMOMessage returnMessage = receiver.routeMessage(message, true); 107 writeResponse(httpServletResponse, returnMessage); 108 109 } 110 catch (Exception e) 111 { 112 handleException(e, "Failed to Post event to Mule", httpServletResponse); 113 } 114 } 115 116 protected void doPut(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 117 throws ServletException , IOException 118 { 119 try 120 { 121 AbstractMessageReceiver receiver = getReceiverForURI(httpServletRequest); 122 httpServletRequest.setAttribute(PAYLOAD_PARAMETER_NAME, payloadParameterName); 123 UMOMessage message = new MuleMessage(receiver.getConnector() 124 .getMessageAdapter(httpServletRequest)); 125 receiver.routeMessage(message, MuleManager.getConfiguration().isSynchronous()); 126 127 httpServletResponse.setStatus(HttpServletResponse.SC_CREATED); 128 if (feedback) 129 { 130 httpServletResponse.getWriter().write( 131 "Item was created at endpointUri: " + receiver.getEndpointURI()); 132 } 133 } 134 catch (Exception e) 135 { 136 handleException(e, "Failed to Post event to Mule" + e.getMessage(), httpServletResponse); 137 } 138 } 139 140 protected void doDelete(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) 141 throws ServletException , IOException 142 { 143 try 144 { 145 UMOEndpoint endpoint = getEndpointForURI(httpServletRequest); 146 String timeoutString = httpServletRequest.getParameter("timeout"); 147 long to = timeout; 148 if (timeoutString != null) 149 { 150 to = new Long (timeoutString).longValue(); 151 } 152 if (logger.isDebugEnabled()) 153 { 154 logger.debug("Making request using endpoint: " + endpoint.toString() + " timeout is: " + to); 155 } 156 157 UMOMessage returnMessage = endpoint.getConnector().getDispatcher(endpoint).receive(endpoint, to); 158 if (returnMessage != null) 159 { 160 httpServletResponse.setStatus(HttpServletResponse.SC_OK); 161 } 162 else 163 { 164 httpServletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT); 165 } 166 } 167 catch (Exception e) 168 { 169 handleException(e, "Failed to Delete mule event via receive using uri: " 170 + httpServletRequest.getPathInfo(), httpServletResponse); 171 } 172 } 173 174 protected UMOEndpoint getEndpointForURI(HttpServletRequest httpServletRequest) 175 throws EndpointException, MalformedEndpointException 176 { 177 String endpointName = httpServletRequest.getParameter("endpoint"); 178 if (endpointName == null) 179 { 180 throw new EndpointException(new Message("http", 8, "endpoint")); 181 } 182 183 UMOEndpoint endpoint = MuleManager.getInstance().lookupEndpoint(endpointName); 184 if (endpoint == null) 185 { 186 UMOMessageReceiver receiver = (UMOMessageReceiver)getReceivers().get(endpointName); 189 if (receiver == null) 190 { 191 throw new EndpointNotFoundException(endpointName); 192 } 193 endpoint = receiver.getEndpoint(); 194 195 } 196 return endpoint; 197 } 198 } 199 | Popular Tags |