| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.ksb.messaging.serviceconnectors; |
| 17 | |
|
| 18 | |
import java.net.URL; |
| 19 | |
import java.util.Map; |
| 20 | |
import java.lang.SuppressWarnings; |
| 21 | |
|
| 22 | |
import org.apache.cxf.binding.BindingFactoryManager; |
| 23 | |
import org.apache.cxf.interceptor.LoggingInInterceptor; |
| 24 | |
import org.apache.cxf.interceptor.LoggingOutInterceptor; |
| 25 | |
import org.apache.cxf.jaxrs.JAXRSBindingFactory; |
| 26 | |
import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean; |
| 27 | |
import org.apache.log4j.Logger; |
| 28 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
| 29 | |
import org.kuali.rice.core.api.security.credentials.CredentialsSource; |
| 30 | |
import org.kuali.rice.ksb.api.bus.support.RestServiceConfiguration; |
| 31 | |
import org.kuali.rice.ksb.messaging.BusClientFailureProxy; |
| 32 | |
import org.kuali.rice.ksb.messaging.bam.BAMClientProxy; |
| 33 | |
import org.kuali.rice.ksb.security.soap.CredentialsOutHandler; |
| 34 | |
import org.kuali.rice.ksb.service.KSBServiceLocator; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
public class ResourceFacadeImpl implements ResourceFacade { |
| 43 | |
|
| 44 | 0 | private static final Logger LOG = Logger.getLogger(ResourceFacadeImpl.class); |
| 45 | |
|
| 46 | |
private final RestServiceConfiguration serviceConfiguration; |
| 47 | |
private CredentialsSource credentialsSource; |
| 48 | |
private URL actualEndpointUrl; |
| 49 | |
|
| 50 | 0 | public ResourceFacadeImpl(final RestServiceConfiguration serviceConfiguration, URL actualEndpointUrl) { |
| 51 | 0 | if (serviceConfiguration == null) { |
| 52 | 0 | throw new IllegalArgumentException("serviceConfiguration cannot be null"); |
| 53 | |
} |
| 54 | 0 | if (actualEndpointUrl == null) { |
| 55 | 0 | throw new IllegalArgumentException("actual endpoint url cannot be null"); |
| 56 | |
} |
| 57 | 0 | this.serviceConfiguration = serviceConfiguration; |
| 58 | 0 | this.actualEndpointUrl = actualEndpointUrl; |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public <R> R getResource(Class<R> resourceClass) { |
| 67 | 0 | if (resourceClass == null) throw new IllegalArgumentException("resourceClass argument must not be null"); |
| 68 | |
|
| 69 | 0 | if (!serviceConfiguration.hasClass(resourceClass.getName())) { |
| 70 | 0 | throw new IllegalArgumentException("Service " + serviceConfiguration.getServiceName() + |
| 71 | |
" does not contain an implementation of type " + resourceClass.getName()); |
| 72 | |
} |
| 73 | |
|
| 74 | 0 | return getServiceProxy(resourceClass); |
| 75 | |
} |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public <R> R getResource(String resourceName) { |
| 83 | |
|
| 84 | 0 | String resourceClassName = null; |
| 85 | |
|
| 86 | 0 | Map<String, String> resourceToClassNameMap = serviceConfiguration.getResourceToClassNameMap(); |
| 87 | |
|
| 88 | 0 | if (resourceName != null && resourceToClassNameMap != null) |
| 89 | 0 | resourceClassName = resourceToClassNameMap.get(resourceName); |
| 90 | |
else |
| 91 | 0 | resourceClassName = serviceConfiguration.getResourceClass(); |
| 92 | |
|
| 93 | 0 | if (resourceClassName == null) |
| 94 | 0 | throw new RiceRuntimeException("No resource class name was found for the specified resourceName: " + resourceName); |
| 95 | |
|
| 96 | 0 | Class<?> resourceClass = null; |
| 97 | |
|
| 98 | |
try { |
| 99 | 0 | resourceClass = Class.forName(resourceClassName); |
| 100 | 0 | } catch (ClassNotFoundException e) { |
| 101 | 0 | throw new RiceRuntimeException("Configured resource class " + resourceClassName + |
| 102 | |
" in service " + serviceConfiguration.getServiceName() + " is not loadable", e); |
| 103 | 0 | } |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
@SuppressWarnings("unchecked") |
| 108 | 0 | R serviceProxy = (R)getServiceProxy(resourceClass); |
| 109 | 0 | return serviceProxy; |
| 110 | |
} |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
private <R> R getServiceProxy(Class<R> resourceClass) { |
| 119 | |
JAXRSClientFactoryBean clientFactory; |
| 120 | |
|
| 121 | 0 | clientFactory = new JAXRSClientFactoryBean(); |
| 122 | 0 | clientFactory.setBus(KSBServiceLocator.getCXFBus()); |
| 123 | |
|
| 124 | 0 | clientFactory.setResourceClass(resourceClass); |
| 125 | 0 | clientFactory.setAddress(actualEndpointUrl.toString()); |
| 126 | 0 | BindingFactoryManager bindingFactoryManager = KSBServiceLocator.getCXFBus().getExtension(BindingFactoryManager.class); |
| 127 | 0 | JAXRSBindingFactory bindingFactory = new JAXRSBindingFactory(); |
| 128 | 0 | bindingFactory.setBus(KSBServiceLocator.getCXFBus()); |
| 129 | |
|
| 130 | 0 | bindingFactoryManager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, bindingFactory); |
| 131 | |
|
| 132 | |
|
| 133 | 0 | if (LOG.isDebugEnabled()) { |
| 134 | 0 | clientFactory.getOutInterceptors().add(new LoggingOutInterceptor()); |
| 135 | |
} |
| 136 | |
|
| 137 | 0 | if (getCredentialsSource() != null) { |
| 138 | 0 | clientFactory.getOutInterceptors().add(new CredentialsOutHandler(getCredentialsSource(), serviceConfiguration)); |
| 139 | |
} |
| 140 | |
|
| 141 | 0 | if (LOG.isDebugEnabled()) { |
| 142 | 0 | clientFactory.getInInterceptors().add(new LoggingInInterceptor()); |
| 143 | |
} |
| 144 | |
|
| 145 | 0 | Object service = clientFactory.create(); |
| 146 | 0 | return getServiceProxyWithFailureMode(resourceClass, service, serviceConfiguration); |
| 147 | |
} |
| 148 | |
|
| 149 | |
public boolean isSingleResourceService() { |
| 150 | 0 | return serviceConfiguration.getResourceToClassNameMap() == null; |
| 151 | |
} |
| 152 | |
|
| 153 | |
public void setCredentialsSource(final CredentialsSource credentialsSource) { |
| 154 | 0 | this.credentialsSource = credentialsSource; |
| 155 | 0 | } |
| 156 | |
|
| 157 | |
protected CredentialsSource getCredentialsSource() { |
| 158 | 0 | return this.credentialsSource; |
| 159 | |
} |
| 160 | |
|
| 161 | |
protected <R> R getServiceProxyWithFailureMode(final Class<R> resourceClass, final Object service, final RestServiceConfiguration serviceConfiguration) { |
| 162 | 0 | Object bamWrappedClientProxy = BAMClientProxy.wrap(service, serviceConfiguration); |
| 163 | 0 | Object proxy = BusClientFailureProxy.wrap(bamWrappedClientProxy, serviceConfiguration); |
| 164 | 0 | if (!resourceClass.isInstance(proxy)) { |
| 165 | 0 | throw new IllegalArgumentException("Wrapped proxy is of the wrong type " + proxy.getClass() + ", expected " + resourceClass); |
| 166 | |
} |
| 167 | 0 | return resourceClass.cast(proxy); |
| 168 | |
} |
| 169 | |
|
| 170 | |
} |