Coverage Report - org.mule.module.jersey.JerseyResourcesComponent
 
Classes in this File Line Coverage Branch Coverage Complexity
JerseyResourcesComponent
0%
0/68
0%
0/22
0
 
 1  
 /*
 2  
  * $Id: JerseyResourcesComponent.java 19270 2010-09-01 10:37:41Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.module.jersey;
 12  
 
 13  
 import org.mule.api.MuleEvent;
 14  
 import org.mule.api.MuleMessage;
 15  
 import org.mule.api.component.JavaComponent;
 16  
 import org.mule.api.endpoint.EndpointURI;
 17  
 import org.mule.api.lifecycle.InitialisationException;
 18  
 import org.mule.api.processor.MessageProcessor;
 19  
 import org.mule.api.transformer.TransformerException;
 20  
 import org.mule.component.AbstractComponent;
 21  
 import org.mule.transport.http.HttpConnector;
 22  
 
 23  
 import com.sun.jersey.api.core.DefaultResourceConfig;
 24  
 import com.sun.jersey.core.header.InBoundHeaders;
 25  
 import com.sun.jersey.core.spi.component.ioc.IoCComponentProviderFactory;
 26  
 import com.sun.jersey.spi.container.ContainerRequest;
 27  
 import com.sun.jersey.spi.container.ContainerResponse;
 28  
 import com.sun.jersey.spi.container.WebApplication;
 29  
 import com.sun.jersey.spi.container.WebApplicationFactory;
 30  
 
 31  
 import java.io.InputStream;
 32  
 import java.net.URI;
 33  
 import java.net.URISyntaxException;
 34  
 import java.util.ArrayList;
 35  
 import java.util.HashSet;
 36  
 import java.util.List;
 37  
 import java.util.Set;
 38  
 
 39  
 import org.apache.commons.logging.Log;
 40  
 import org.apache.commons.logging.LogFactory;
 41  
 
 42  
 /**
 43  
  * Wraps a set of components which can get invoked by Jersey. This component
 44  
  * will maps the MuleMessage format to the internal Jersey format. Jersey will then select
 45  
  * the appropriate component to invoke based on the request parameters/URI.
 46  
  */
 47  0
 public class JerseyResourcesComponent extends AbstractComponent
 48  
 {
 49  0
     public static String JERSEY_RESPONSE = "jersey_response";
 50  
 
 51  0
     protected final Log logger = LogFactory.getLog(this.getClass());
 52  
 
 53  
     private List<JavaComponent> components;
 54  
 
 55  
     private WebApplication application;
 56  
 
 57  
     @Override
 58  
     protected void doInitialise() throws InitialisationException
 59  
     {
 60  0
         super.doInitialise();
 61  
         
 62  0
         final Set<Class<?>> resources = new HashSet<Class<?>>();
 63  
 
 64  0
         if (components == null) 
 65  
         {
 66  0
             throw new IllegalStateException("There must be at least one component in the Jersey resources.");
 67  
         }
 68  
         
 69  
         // Initialize the Jersey resources using the components
 70  0
         for (JavaComponent component : components)
 71  
         {
 72  
             Class<?> c;
 73  
             try
 74  
             {
 75  0
                 c = component.getObjectType();
 76  0
                 resources.add(c);
 77  
             }
 78  0
             catch (Exception e)
 79  
             {
 80  0
                 throw new InitialisationException(e, this);
 81  0
             }
 82  0
         }
 83  
 
 84  0
         DefaultResourceConfig resourceConfig = createConfiguration(resources);
 85  
 
 86  0
         application = WebApplicationFactory.createWebApplication();
 87  0
         application.initiate(resourceConfig, getComponentProvider());
 88  0
     }
 89  
 
 90  
     protected DefaultResourceConfig createConfiguration(final Set<Class<?>> resources)
 91  
     {
 92  0
         return new DefaultResourceConfig(resources);
 93  
     }
 94  
 
 95  
     @Override
 96  
     protected Object doInvoke(MuleEvent event) throws Exception
 97  
     {
 98  0
         MuleMessage message = event.getMessage();
 99  
 
 100  0
         String path = (String) message.getInboundProperty(HttpConnector.HTTP_REQUEST_PROPERTY);
 101  0
         String contextPath = (String) message.getInboundProperty(HttpConnector.HTTP_CONTEXT_PATH_PROPERTY);
 102  0
         String query = null;
 103  0
         int queryIdx = path.indexOf('?');
 104  0
         if (queryIdx != -1)
 105  
         {
 106  0
             query = path.substring(queryIdx + 1);
 107  0
             path = path.substring(0, queryIdx);
 108  
         }
 109  
 
 110  0
         EndpointURI endpointUri = event.getEndpoint().getEndpointURI();
 111  0
         String host = message.getInboundProperty("Host", endpointUri.getHost());
 112  0
         String method = message.getInboundProperty(HttpConnector.HTTP_METHOD_PROPERTY);
 113  0
         InBoundHeaders headers = new InBoundHeaders();
 114  0
         for (Object prop : message.getInboundPropertyNames())
 115  
         {
 116  0
             Object property = message.getInboundProperty(prop.toString());
 117  0
             if (property != null) 
 118  
             {
 119  0
                 headers.add(prop.toString(), property.toString());
 120  
             }
 121  0
         }
 122  
 
 123  
         String scheme;
 124  0
         if ("servlet".equals(endpointUri.getScheme()))
 125  
         {
 126  0
             scheme = "http";
 127  
         }
 128  
         else
 129  
         {
 130  0
             scheme = endpointUri.getScheme();
 131  
         }
 132  
 
 133  0
         URI baseUri = getBaseUri(endpointUri, scheme, host, contextPath);
 134  0
         URI completeUri = getCompleteUri(endpointUri, scheme, host, path, query);
 135  0
         ContainerRequest req = new ContainerRequest(application, method, baseUri, completeUri, headers,
 136  
             getInputStream(message));
 137  0
         if (logger.isDebugEnabled())
 138  
         {
 139  0
             logger.debug("Base URI: " + baseUri);
 140  0
             logger.debug("Complete URI: " + completeUri);
 141  
         }
 142  
 
 143  0
         MuleResponseWriter writer = new MuleResponseWriter(message);
 144  0
         ContainerResponse res = new ContainerResponse(application, req, writer);
 145  
 
 146  0
         application.handleRequest(req, res);
 147  
         
 148  0
         return writer.getResponse();
 149  
     }
 150  
 
 151  
     protected static InputStream getInputStream(MuleMessage message) throws TransformerException
 152  
     {
 153  0
         return message.getPayload(InputStream.class);
 154  
     }
 155  
 
 156  
     protected IoCComponentProviderFactory getComponentProvider()
 157  
     {
 158  0
         return new MuleComponentProviderFactory(muleContext, components);
 159  
     }
 160  
 
 161  
     protected static URI getCompleteUri(EndpointURI endpointUri,
 162  
                                         String scheme,
 163  
                                         String host,
 164  
                                         String path,
 165  
                                         String query) throws URISyntaxException
 166  
     {
 167  0
         String uri = scheme + "://" + host + path;
 168  0
         if (query != null)
 169  
         {
 170  0
             uri += "?" + query;
 171  
         }
 172  
 
 173  0
         return new URI(uri);
 174  
     }
 175  
 
 176  
     protected static URI getBaseUri(EndpointURI endpointUri, String scheme, String host, String contextPath)
 177  
         throws URISyntaxException
 178  
     {
 179  0
         if (!contextPath.endsWith("/"))
 180  
         {
 181  0
             contextPath += "/";
 182  
         }
 183  
 
 184  0
         return new URI(scheme + "://" + host + contextPath);
 185  
     }
 186  
 
 187  
     public List<JavaComponent> getComponents()
 188  
     {
 189  0
         return components;
 190  
     }
 191  
 
 192  
     public void setComponents(List<JavaComponent> components)
 193  
     {
 194  0
         this.components = components;
 195  0
     }
 196  
     
 197  
     public void setMessageProcessors(List<MessageProcessor> messageProcessors) 
 198  
     {
 199  0
         List<JavaComponent> components = new ArrayList<JavaComponent>();
 200  0
         for (MessageProcessor mp : messageProcessors) 
 201  
         {
 202  0
             if (mp instanceof JavaComponent) 
 203  
             {
 204  0
                 components.add((JavaComponent)mp);
 205  
             }
 206  
             else
 207  
             {
 208  0
                 throw new IllegalStateException("Only JavaComponents are allowed as MessageProcessors. Type " + mp.getClass().getName() + " is not allowed.");
 209  
             }
 210  
         }
 211  0
         setComponents(components);
 212  0
     }
 213  
 }