/** * Determine which mime-type and corresponding marshaller is mutually supported. * * @param remoteSupportedMimeTypes the mime-types supported by the remote server * @return a {@link IMarshaller} or null * @throws ServiceException */ public static IMarshaller determineCommonMimeType(final List<String> remoteSupportedMimeTypes) throws ServiceException { // determine which mime-type and corresponding marshaller is mutually // supported by this lookup and the // registry-server IMarshaller marshaller = null; for (final String mimeType : remoteSupportedMimeTypes) { marshaller = MarshallerFactory.get(mimeType); if (marshaller != null) { break; } } // check if a common mime-type was found if (marshaller == null) { throw new ServiceException("Could not find common mime-type to communicate with. A marshaller for one of the following mime-types must be provided: " + remoteSupportedMimeTypes); } return marshaller; } }
/** * Create a service client which proxies the calls to the service server. * * @param serviceDescriptor the {@link ServiceDescriptor} * @return the client to the server * @throws ClassNotFoundException */ @SuppressWarnings("unchecked") protected final <T> T createServiceClient(final ServiceDescriptor serviceDescriptor) throws ServiceException { // find common mime-types final List<String> supportedMimeTypes = new ArrayList<>(serviceDescriptor.getSupportedMimeTypes()); supportedMimeTypes.retainAll(MarshallerFactory.getSupportedMimeTypes()); if (supportedMimeTypes.isEmpty()) { throw new ServiceException("No common mime-types for client and server! Server supports: " + serviceDescriptor.getSupportedMimeTypes() + ", client: " + MarshallerFactory.getSupportedMimeTypes()); } try { // load the service-interface class to create a proxy for final Class<?> serviceInterface = Class.forName(serviceDescriptor.getServiceInterface()); // get compatible marshaller to serialize the request content final IMarshaller marshaller = MarshallerFactory.get(supportedMimeTypes.get(0)); return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { serviceInterface }, new ServiceClientInvocationHandler(serviceDescriptor, marshaller)); } catch (final Exception e) { throw new ServiceException("Could not create service-client!", e); } }
/** * Unmarshal the {@link ServiceDescriptor} from the request input-stream. * * @param headers the request headers * @param inputStream the {@link InputStream} * @return the {@link ServiceDescriptor} * @throws ServiceException */ protected <T> T unmarshal(final String mimeType, final InputStream inputStream, final Class<T> clazz) throws ServiceException { T result = null; // try to get a marshaller configured to handle the request content-type final IMarshaller marshaller = MarshallerFactory.get(mimeType); if (marshaller != null) { // deserialize result = marshaller.deserialize(inputStream, clazz); } else { LOG.error("No marshaller found for mime-type: " + mimeType); throw new ServiceException("No marshaller found for mime-type: " + mimeType); } return result; }
@Override public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable { // create a method call final MethodCall call = MethodCall.create(method.getName(), args); // send the request to the remote service final HttpResponse response = ServiceUtils.executeRequest(serviceDescriptor, serviceDescriptor.getServiceUri(), call); if (response.getEntity() != null && response.getEntity().getContentLength() > 0 && response.getHeaders(HttpHeaders.CONTENT_TYPE) != null && response.getHeaders(HttpHeaders.CONTENT_TYPE).length > 0) { // deserialize with the marshaller for the response content-type final String mimeType = ServiceUtils.getMimeTypeFromResponse(response); final IMarshaller responseMarshaller = MarshallerFactory.get(mimeType); if (responseMarshaller == null) { LOG.error("Could not find a marshaller for the response content-type: " + mimeType); throw new ServiceException("Could not find a marshaller for the response content-type: " + mimeType); } return responseMarshaller.deserialize(response.getEntity().getContent(), method.getReturnType()); } return null; }
/** * Gets a {@link IMarshaller} by querying the REST server's supported mime-types and trying to * find a match by the clients supported mime-types. * * @param host the host of the REST server * @param port the port of the REST server * @param restServerUri the URI which returns the mime-types in text/plain * @return the {@link IMarshaller} * @throws ServiceException */ public static IMarshaller getMarshallerToCommunicateWithRestServer(final String host, final int port, final String restServerUri) throws ServiceException { // first determine which mime-types the rest server supports final List<String> registryServerMimeTypes = ServiceUtils.getSupportedMediaTypesOfRestServer(host, port, restServerUri); // try to get a matching marshaller for the supported mime-type IMarshaller marshaller = null; for (final String mimeType : registryServerMimeTypes) { marshaller = MarshallerFactory.get(mimeType); if (marshaller != null) { break; } } // check if a common mime-type was found if (marshaller == null) { throw new ServiceException("Could not find common mime-type to communicate with reqistry server. A marshaller for one of the following mime-types must be provided: " + registryServerMimeTypes); } return marshaller; }
final IMarshaller marshaller = MarshallerFactory.get(mimeType);
final IMarshaller responseMarshaller = MarshallerFactory.get(mimeType);