1 10 11 package org.mule.providers.http.servlet; 12 13 import org.mule.config.i18n.Message; 14 import org.mule.impl.MuleMessage; 15 import org.mule.impl.endpoint.MuleEndpointURI; 16 import org.mule.providers.AbstractMessageReceiver; 17 import org.mule.providers.http.HttpConnector; 18 import org.mule.providers.service.ConnectorFactory; 19 import org.mule.umo.UMOMessage; 20 import org.mule.umo.endpoint.EndpointException; 21 import org.mule.umo.provider.NoReceiverForEndpointException; 22 import org.mule.util.PropertiesUtils; 23 import org.mule.MuleManager; 24 25 import javax.servlet.ServletConfig ; 26 import javax.servlet.ServletException ; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpServletResponse ; 29 30 import java.io.IOException ; 31 import java.util.Map ; 32 import java.util.Properties ; 33 34 42 43 public class MuleReceiverServlet extends AbstractReceiverServlet 44 { 45 48 private static final long serialVersionUID = 6631307373079767439L; 49 50 protected ServletConnector connector = null; 51 52 protected void doInit(ServletConfig servletConfig) throws ServletException 53 { 54 String servletConnectorName = servletConfig.getInitParameter(SERVLET_CONNECTOR_NAME_PROPERTY); 55 if(servletConnectorName==null) 56 { 57 connector = (ServletConnector)ConnectorFactory.getConnectorByProtocol("servlet"); 58 if (connector == null) 59 { 60 throw new ServletException (new Message("http", 9).toString()); 61 } 62 } 63 else 64 { 65 connector = (ServletConnector)MuleManager.getInstance().lookupConnector(servletConnectorName); 66 if (connector == null) 67 { 68 throw new ServletException (new Message("http", 10, servletConnectorName).toString()); 69 } 70 } 71 72 73 } 74 75 protected void doGet(HttpServletRequest request, HttpServletResponse response) 76 throws ServletException , IOException 77 { 78 try 79 { 80 AbstractMessageReceiver receiver = getReceiverForURI(request); 81 UMOMessage responseMessage; 82 UMOMessage requestMessage = new MuleMessage(new HttpRequestMessageAdapter(request)); 83 requestMessage.setProperty(HttpConnector.HTTP_METHOD_PROPERTY, "GET"); 84 responseMessage = receiver.routeMessage(requestMessage, true); 85 writeResponse(response, responseMessage); 86 } 87 catch (Exception e) 88 { 89 handleException(e, e.getMessage(), response); 90 } 91 } 92 93 protected void doPost(HttpServletRequest request, HttpServletResponse response) 94 throws ServletException , IOException 95 { 96 try 97 { 98 AbstractMessageReceiver receiver = getReceiverForURI(request); 99 UMOMessage responseMessage; 100 UMOMessage requestMessage = new MuleMessage(new HttpRequestMessageAdapter(request)); 101 requestMessage.setProperty(HttpConnector.HTTP_METHOD_PROPERTY, "POST"); 102 responseMessage = receiver.routeMessage(requestMessage, true); 103 if (responseMessage != null) 104 { 105 writeResponse(response, responseMessage); 106 } 107 } 108 catch (Exception e) 109 { 110 handleException(e, e.getMessage(), response); 111 } 112 } 113 114 protected AbstractMessageReceiver getReceiverForURI(HttpServletRequest httpServletRequest) 115 throws EndpointException 116 { 117 String uri = getReceiverName(httpServletRequest); 118 if (uri == null) 119 { 120 throw new EndpointException(new Message("http", 4, httpServletRequest.getRequestURI())); 121 } 122 AbstractMessageReceiver receiver = (AbstractMessageReceiver)getReceivers().get(uri); 123 if (receiver == null) 124 { 125 int i = uri.lastIndexOf("/"); 128 if (i > -1) 129 { 130 uri = uri.substring(i + 1); 131 receiver = (AbstractMessageReceiver)getReceivers().get(uri); 132 } 133 if (receiver == null) 134 { 135 throw new NoReceiverForEndpointException("No receiver found for endpointUri: " + uri); 136 } 137 } 138 receiver.getEndpoint().setEndpointURI(new MuleEndpointURI(getRequestUrl(httpServletRequest))); 139 return receiver; 140 } 141 142 protected String getRequestUrl(HttpServletRequest httpServletRequest) 143 { 144 StringBuffer url = new StringBuffer (); 145 url.append(connector.getProtocol().toLowerCase()); 146 url.append(":"); 147 url.append(httpServletRequest.getScheme()); 148 url.append("://"); 149 url.append(httpServletRequest.getServerName()); 150 url.append(":"); 151 url.append(httpServletRequest.getServerPort()); 152 url.append("/"); 153 url.append(getReceiverName(httpServletRequest)); 154 if (httpServletRequest.getQueryString() != null) 155 { 156 url.append("?"); 157 url.append(httpServletRequest.getQueryString()); 158 } 159 return url.toString(); 160 } 161 162 protected String getReceiverName(HttpServletRequest httpServletRequest) 163 { 164 String name = httpServletRequest.getPathInfo(); 165 if (name == null) 166 { 167 name = httpServletRequest.getServletPath(); 168 if (name == null) 169 { 170 name = httpServletRequest.getParameter("endpoint"); 171 if (name == null) 172 { 173 Properties params = PropertiesUtils.getPropertiesFromQueryString(httpServletRequest.getQueryString()); 174 name = params.getProperty("endpoint"); 175 if (name == null) 176 { 177 return null; 178 } 179 } 180 } 181 } 182 183 if (name.startsWith("/")) 184 { 185 name = name.substring(1); 186 } 187 return name; 188 } 189 190 protected Map getReceivers() 191 { 192 return connector.getReceivers(); 193 } 194 } 195 | Popular Tags |