long getLatency(final AbstractDescriptor descriptor) { long latency = Long.MAX_VALUE; try { final long startTime = System.currentTimeMillis(); ServiceUtils.executeRequest(descriptor, descriptor.getHeartbeatUri(), null); latency = System.currentTimeMillis() - startTime; } catch (final ServiceException e) { LOG.warn("Heartbeat to " + descriptor + " failed!"); } return latency; }
public void removeService(final String serviceId, final String serviceVersion) throws ServiceException { final HttpPost post = new HttpPost(ServiceUtils.buildURL(host, port, useSSL, contextRoot + "/server/remove/" + serviceId + "/" + serviceVersion)); try { ServiceUtils.executeRequest(post, useSSL); } catch (final ServiceException e) { throw e; } catch (final Exception e) { throw new ServiceException("Could not remove service " + serviceId + ":" + serviceVersion, e); } }
/** * Register the specified {@link ServiceDescriptor} with the registry servers. * * @param serviceDescriptors the {@link ServiceDescriptor} * @throws ServiceException */ public void registerService(final ServiceDescriptor serviceDescriptor) throws ServiceException { // notify all know registries about the new service for (final RegistryDescriptor registryDescriptor : RegistryDiscovererService.requestRegistries()) { try { ServiceUtils.executeRequest(registryDescriptor, registryDescriptor.getRegisterUri(), serviceDescriptor); } catch (final Exception e) { LOG.error("Unable to register: " + serviceDescriptor, e); throw new ServiceException("Unable to register: " + serviceDescriptor, e); } } }
/** * Register the specified {@link ServiceDescriptor} with the registry-server. * * @param serviceDescriptors the {@link ServiceDescriptor} * @throws ServiceException */ public void unregisterService(final ServiceDescriptor serviceDescriptor) throws ServiceException { // notify all know registries about the removed service for (final RegistryDescriptor registryDescriptor : RegistryDiscovererService.requestRegistries()) { try { ServiceUtils.executeRequest(registryDescriptor, registryDescriptor.getUnregisterUri(), serviceDescriptor); } catch (final Exception e) { LOG.error("Unable to unregister: " + serviceDescriptor, e); throw new ServiceException("Unable to unregister: " + serviceDescriptor, e); } } }
@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; }
/** * Propagates the specified service to all other known registries. The {@link Action} specifies * whether it was added or removed. * * @param propagateServiceDescriptor the {@link ServiceDescriptor} of the registry * @param action the {@link Action} */ void propagateServiceToOtherRegistries(final ServiceDescriptor propagateServiceDescriptor, final Action action) { // propagate the service to all other known registries for (final RegistryDescriptor otherRegistryDescriptor : registryCache.getAll()) { try { WebCall webCall = null; switch (action) { case ADD: webCall = otherRegistryDescriptor.getRegisterUri(); break; case REMOVE: webCall = otherRegistryDescriptor.getUnregisterUri(); break; } ServiceUtils.executeRequest(otherRegistryDescriptor, webCall, propagateServiceDescriptor); } catch (final Exception e) { LOG.warn("Unable to propagate service (" + propagateServiceDescriptor + ") to registry: " + registryDescriptor, e); } } }
/** * Add the service for the specified service-bundle to the RestServiceServer at the host and port. * * @param serviceBundlePath the path to the service-bundle * @return the {@link ServiceDescriptor} filled with server parameters * @throws ServiceException */ public RestServiceServerUtil addService(final Path serviceBundlePath) throws ServiceException { try { final HttpPost post = new HttpPost(ServiceUtils.buildURL(host, port, useSSL, contextRoot + "/server/add")); // post.addHeader(HttpHeaders.CONTENT_TYPE, marshaller.getMimeType()); post.addHeader(HttpHeaders.ACCEPT, ServiceUtils.getAcceptedMimeTypes()); post.setEntity(EntityBuilder.create().setBinary(Files.readAllBytes(serviceBundlePath)).build()); ServiceUtils.executeRequest(post, useSSL); return this; } catch (final ServiceException e) { throw e; } catch (final Exception e) { throw new ServiceException("Could not add service-bundle " + serviceBundlePath, e); } }
public static HttpResponse executeRequest(final AbstractDescriptor descriptor, final WebCall webCall, final Serializable entity) throws ServiceException { return executeRequest(descriptor.getHost(), descriptor.getPort(), descriptor.isUseSSL(), webCall, descriptor.getSupportedMimeTypes(), entity); }
/** * Propagates the specified registry to all other known registries. The {@link Action} specifies * whether it was added or removed. * * @param propagateRegistryDescriptor the {@link RegistryDescriptor} of the registry * @param action the {@link Action} */ void propagateRegistryToOtherRegistries(final RegistryDescriptor propagateRegistryDescriptor, final Action action) { // propagate the new registry to all other known registries for (final RegistryDescriptor otherRegistryDescriptor : registryCache.getAll()) { if (!otherRegistryDescriptor.equals(propagateRegistryDescriptor)) { try { WebCall webCall = null; switch (action) { case ADD: webCall = otherRegistryDescriptor.getAddRegistryUri(); break; case REMOVE: webCall = otherRegistryDescriptor.getRemoveRegistryUri(); break; } ServiceUtils.executeRequest(otherRegistryDescriptor, webCall, propagateRegistryDescriptor); } catch (final Exception e) { LOG.warn("Unable to propagate registry (" + propagateRegistryDescriptor + ") to registry: " + registryDescriptor, e); } } } }
ServiceUtils.executeRequest(registryDescriptor, webCall, serviceDescriptor); } catch (final Exception e) { LOG.warn("Service (" + serviceDescriptor + ") could not be propagated to registry (" + registryDescriptor + ")", e);
final HttpResponse response = ServiceUtils.executeRequest(registryDescriptor, WebCall.create(registryDescriptor.getServiceUri().getUri() + "/" + serviceId + "/" + serviceVersion, registryDescriptor.getServiceUri().getWebMethod()), null);
public static HttpResponse executeRequest(final String host, final int port, final boolean useSSL, final WebCall webCall, final List<String> remoteSupportedMimeTypes, final Serializable entity) throws ServiceException { HttpUriRequest request = null; IMarshaller marshaller = null; // determine the marshaller base on the specified remote mime-types if (remoteSupportedMimeTypes != null) { marshaller = determineCommonMimeType(remoteSupportedMimeTypes); } else if (webCall.getWebMethod().equalsIgnoreCase("POST")) { // if no mime-types were specified and its a POST request, then // throw exception - cannot serialize object without marshaller throw new ServiceException("Cannot call POST web-method without specifying the supported mime-types of the remote server!"); } if (webCall.getWebMethod().equalsIgnoreCase("GET")) { request = new HttpGet(buildURL(host, port, useSSL, webCall.getUri())); } else if (webCall.getWebMethod().equalsIgnoreCase("POST")) { request = new HttpPost(buildURL(host, port, useSSL, webCall.getUri())); ((HttpPost) request).setEntity(EntityBuilder.create().setBinary(marshaller.serialize(entity)).build()); } else if (webCall.getWebMethod().equalsIgnoreCase("DELETE")) { request = new HttpGet(buildURL(host, port, useSSL, webCall.getUri())); } else { throw new ServiceException("Unsupported web-call web method: " + webCall); } if (marshaller != null) { request.addHeader(HttpHeaders.CONTENT_TYPE, marshaller.getMimeType()); } request.addHeader(HttpHeaders.ACCEPT, getAcceptedMimeTypes()); return executeRequest(request, useSSL); }