/** * Send message to webapp to refresh SearcherManagers for given projects. * This is used for partial reindex. * * @param subFiles list of directories to refresh corresponding SearcherManagers * @param host the host address to receive the configuration */ public void signalTorefreshSearcherManagers(List<String> subFiles, String host) { // subFile entries start with path separator so get basename // to convert them to project names. subFiles.stream().map(proj -> new File(proj).getName()).forEach(project -> { Response r = ClientBuilder.newClient() .target(host) .path("api") .path("v1") .path("system") .path("refresh") .request() .put(Entity.text(project)); if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { LOGGER.log(Level.WARNING, "Could not refresh search manager for {0}", project); } }); }
/** * Write the current configuration to a socket * * @param host the host address to receive the configuration * @throws IOException if an error occurs */ public void writeConfiguration(String host) throws IOException { String configXML; try { configLock.readLock().lock(); configXML = configuration.getXMLRepresentationAsString(); } finally { configLock.readLock().unlock(); } Response r = ClientBuilder.newClient() .target(host) .path("api") .path("v1") .path("configuration") .queryParam("reindex", true) .request() .put(Entity.xml(configXML)); if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { throw new IOException(r.toString()); } }
public DirectoryReconciliationResponse sendChunk(DirectoryReconciliationRequest request) { return client.target(replicationUrl) .path("/v1/directory/reconcile") .request(MediaType.APPLICATION_JSON_TYPE) .put(Entity.json(request), DirectoryReconciliationResponse.class); }
@Put public ResponseValue putXml(Path path, QueryParam queryParam, String xmlParam, HeaderParam headerParam) { Objects.requireNonNull(xmlParam, "Xml is null."); WebTarget target = this.target; if (path != null) { target = path.appendToTarget(this.target); } if (queryParam != null) { target = queryParam.appendToTarget(target); } Builder request = target.request(); if (headerParam != null) { request = headerParam.appendToRequest(request); } try { this.logger.debug("Prepare to put to {} with query {}, body {} and header {}.", path, queryParam, xmlParam, headerParam); Response r = request.put(Entity.xml(xmlParam), Response.class); return toResponseValue(r); } catch (ProcessingException | WebApplicationException e) { this.logger.error("Fail to put.", e); return ResponseValue.internalServerError(); } }
@Override public void start(final String keywords, final DataListener msgListener) { msgListener.onStart(); running = true; final Random rnd = new Random(); final String aggregatorPrefix = getPrefix(); Executors.newSingleThreadExecutor().submit(() -> { final Client resourceClient = ClientBuilder.newClient(); resourceClient.register(new MoxyJsonFeature()); final WebTarget messageStreamResource = resourceClient.target(App.getApiUri()).path(getPath()); try { while (running) { final Message message = new Message( aggregatorPrefix + " " + MESSAGES[rnd.nextInt(MESSAGES.length)], rgbColor, IMG_URI); msgListener.onMessage(message); final Response r = messageStreamResource.request().put(Entity.json(message)); if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { LOGGER.warning("Unexpected PUT message response status code: " + r.getStatus()); } Thread.sleep(rnd.nextInt(1000) + 750); } msgListener.onComplete(); } catch (Throwable t) { LOGGER.log(Level.WARNING, "Waiting for a message has been interrupted.", t); msgListener.onError(); } }); }
private void markProjectIndexed(Project project) { RuntimeEnvironment env = RuntimeEnvironment.getInstance(); // Successfully indexed the project. The message is sent even if // the project's isIndexed() is true because it triggers RepositoryInfo // refresh. if (project != null) { // Also need to store the correct value in configuration // when indexer writes it to a file. project.setIndexed(true); if (env.getConfigURI() != null) { Response r = ClientBuilder.newClient() .target(env.getConfigURI()) .path("api") .path("v1") .path("projects") .path(Util.URIEncode(project.getName())) .path("indexed") .request() .put(Entity.text("")); if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { LOGGER.log(Level.WARNING, "Couldn''t notify the webapp that project {0} was indexed: {1}", new Object[] {project, r}); } } } }
@Override public ScheduleComponentsEntity scheduleProcessGroupComponents( final String processGroupId, final ScheduleComponentsEntity scheduleComponentsEntity) throws NiFiClientException, IOException { if (StringUtils.isBlank(processGroupId)) { throw new IllegalArgumentException("Process group id cannot be null"); } if (scheduleComponentsEntity == null) { throw new IllegalArgumentException("ScheduleComponentsEntity cannot be null"); } scheduleComponentsEntity.setId(processGroupId); return executeAction("Error scheduling components", () -> { final WebTarget target = flowTarget .path("process-groups/{id}") .resolveTemplate("id", processGroupId); return getRequestBuilder(target).put( Entity.entity(scheduleComponentsEntity, MediaType.APPLICATION_JSON_TYPE), ScheduleComponentsEntity.class); }); }
@Override public void run() { final Client resourceClient = ClientBuilder.newClient(); resourceClient.register(new MoxyJsonFeature()); final WebTarget messageStreamResource = resourceClient.target(App.getApiUri()).path("message/stream"); Message message = null; try { while (!cancelled && (message = messages.take()) != null) { msgListener.onMessage(message); final Response r = messageStreamResource.request().put(Entity.json(message)); if (r.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { LOGGER.warning("Unexpected PUT message response status code: " + r.getStatus()); } } if (message == null) { LOGGER.info("Timed out while waiting for a message."); } } catch (InterruptedException ex) { LOGGER.log(Level.WARNING, "Waiting for a message has been interrupted.", ex); } finally { readerHandle.cancel(true); msgListener.onComplete(); } } });
@Override public RegistryClientEntity updateRegistryClient(final RegistryClientEntity registryClient) throws NiFiClientException, IOException { if (registryClient == null) { throw new IllegalArgumentException("Registry client entity cannot be null"); } if (StringUtils.isBlank(registryClient.getId())) { throw new IllegalArgumentException("Registry client entity must contain an id"); } return executeAction("Error updating registry client", () -> { final WebTarget target = controllerTarget .path("registry-clients/{id}") .resolveTemplate("id", registryClient.getId()); return getRequestBuilder(target).put( Entity.entity(registryClient, MediaType.APPLICATION_JSON), RegistryClientEntity.class ); }); }
@Override public ActivateControllerServicesEntity activateControllerServices(final ActivateControllerServicesEntity activateControllerServicesEntity) throws NiFiClientException, IOException { if (activateControllerServicesEntity == null) { throw new IllegalArgumentException("Entity cannot be null"); } if (StringUtils.isBlank(activateControllerServicesEntity.getId())) { throw new IllegalArgumentException("Entity must contain a process group id"); } return executeAction("Error enabling or disabling controlling services", () -> { final WebTarget target = flowTarget .path("process-groups/{id}/controller-services") .resolveTemplate("id", activateControllerServicesEntity.getId()); return getRequestBuilder(target).put( Entity.entity(activateControllerServicesEntity, MediaType.APPLICATION_JSON_TYPE), ActivateControllerServicesEntity.class); }); }
@Override public ProcessGroupEntity updateProcessGroup(final ProcessGroupEntity entity) throws NiFiClientException, IOException { if (entity == null){ throw new IllegalArgumentException("Process group entity cannot be null"); } return executeAction("Error updating process group", () -> { final WebTarget target = processGroupsTarget .path("{id}") .resolveTemplate("id", entity.getId()); return getRequestBuilder(target).put( Entity.entity(entity, MediaType.APPLICATION_JSON_TYPE), ProcessGroupEntity.class ); }); }
response = requestBuilder.accept(MediaType.APPLICATION_JSON_TYPE).put(Entity.entity("{}", MediaType.APPLICATION_JSON_TYPE)); // Jersey2 refuses to handle PUT with no body InstanceInfo infoFromPeer = null; if (response.getStatus() == Status.CONFLICT.getStatusCode() && response.hasEntity()) {
addExtraHeaders(requestBuilder); requestBuilder.accept(MediaType.APPLICATION_JSON_TYPE); response = requestBuilder.put(Entity.entity("{}", MediaType.APPLICATION_JSON_TYPE)); // Jersey2 refuses to handle PUT with no body EurekaHttpResponseBuilder<InstanceInfo> eurekaResponseBuilder = anEurekaHttpResponse(response.getStatus(), InstanceInfo.class).headers(headersOf(response)); if (response.hasEntity()) {
@Override public NodeEntity connectNode(final String nodeId, final NodeEntity nodeEntity) throws NiFiClientException, IOException { if (StringUtils.isBlank(nodeId)) { throw new IllegalArgumentException("Node ID cannot be null or empty"); } if (nodeEntity == null) { throw new IllegalArgumentException("Node entity cannot be null"); } return executeAction("Error connecting node", () -> { final WebTarget target = controllerTarget.path("cluster/nodes/" + nodeId); return getRequestBuilder(target).put(Entity.entity(nodeEntity, MediaType.APPLICATION_JSON), NodeEntity.class); }); }
@Override public NodeEntity disconnectNode(final String nodeId, final NodeEntity nodeEntity) throws NiFiClientException, IOException { if (StringUtils.isBlank(nodeId)) { throw new IllegalArgumentException("Node ID cannot be null or empty"); } if (nodeEntity == null) { throw new IllegalArgumentException("Node entity cannot be null"); } return executeAction("Error disconnecting node", () -> { final WebTarget target = controllerTarget.path("cluster/nodes/" + nodeId); return getRequestBuilder(target).put(Entity.entity(nodeEntity, MediaType.APPLICATION_JSON), NodeEntity.class); }); }
@Override public NodeEntity offloadNode(final String nodeId, final NodeEntity nodeEntity) throws NiFiClientException, IOException { if (StringUtils.isBlank(nodeId)) { throw new IllegalArgumentException("Node ID cannot be null or empty"); } if (nodeEntity == null) { throw new IllegalArgumentException("Node entity cannot be null"); } return executeAction("Error offloading node", () -> { final WebTarget target = controllerTarget.path("cluster/nodes/" + nodeId); return getRequestBuilder(target).put(Entity.entity(nodeEntity, MediaType.APPLICATION_JSON), NodeEntity.class); }); }
public <V> T put(final String queryResourceUrl, final V request) { CompletableFuture<T> result = new CompletableFuture<>(); final WebTarget webResource = client.target(queryResourceUrl); return buildResponse(webResource .request(accept) .accept(accept) .put(Entity.entity(request, contentType),String.class)); } public T delete(final String queryResourceUrl) {
@Override protected Void execute(CopyArchiveToContainerCmd command) { WebTarget webResource = getBaseResource().path("/containers/{id}/archive").resolveTemplate("id", command.getContainerId()); LOGGER.trace("PUT: " + webResource.toString()); InputStream streamToUpload = command.getTarInputStream(); webResource.queryParam("path", command.getRemotePath()) .queryParam("noOverwriteDirNonDir", command.isNoOverwriteDirNonDir()).request() .put(entity(streamToUpload, "application/x-tar")).close(); return null; } }
@Override public EurekaHttpResponse<Void> statusUpdate(String appName, String id, InstanceStatus newStatus, InstanceInfo info) { String urlPath = "apps/" + appName + '/' + id + "/status"; Response response = null; try { Builder requestBuilder = jerseyClient.target(serviceUrl) .path(urlPath) .queryParam("value", newStatus.name()) .queryParam("lastDirtyTimestamp", info.getLastDirtyTimestamp().toString()) .request(); addExtraProperties(requestBuilder); addExtraHeaders(requestBuilder); response = requestBuilder.put(Entity.entity("{}", MediaType.APPLICATION_JSON_TYPE)); // Jersey2 refuses to handle PUT with no body return anEurekaHttpResponse(response.getStatus()).headers(headersOf(response)).build(); } finally { if (logger.isDebugEnabled()) { logger.debug("Jersey2 HTTP PUT {}/{}; statusCode={}", serviceUrl, urlPath, response == null ? "N/A" : response.getStatus()); } if (response != null) { response.close(); } } }
@Override public EurekaHttpResponse<Void> statusUpdate(String asgName, ASGStatus newStatus) { Response response = null; try { String urlPath = "asg/" + asgName + "/status"; response = jerseyClient.target(serviceUrl) .path(urlPath) .queryParam("value", newStatus.name()) .request() .header(PeerEurekaNode.HEADER_REPLICATION, "true") .put(Entity.text("")); return EurekaHttpResponse.status(response.getStatus()); } finally { if (response != null) { response.close(); } } }