/** * Creates a new Kie Controller Client using REST based service * @param controllerUrl the URL to the server (e.g.: "http://localhost:8080/kie-server-controller/rest/controller") * @param login user login * @param password user password * @return client instance */ public static KieServerControllerClient newRestClient(final String controllerUrl, final String login, final String password) { return new RestKieServerControllerClient(controllerUrl, login, password); }
private RuntimeException createExceptionForUnexpectedFailure(WebTarget request, Exception e) { String summaryMessage = "Unexpected exception when requesting URI '" + getClientRequestUri(request) + "'!"; logger.debug(summaryMessage); return new RuntimeException(summaryMessage, e); }
@Override public void deleteServerTemplate(String serverTemplateId) { makeDeleteRequest(controllerBaseUrl + MANAGEMENT_URI_PART + serverTemplateId); }
private <T> T makePostRequestAndCreateCustomResponse(String uri, Object bodyObject, Class<T> resultType) { WebTarget clientRequest = httpClient.target(uri); Response response; try { Entity<String> requestEntity = Entity.entity(serialize(bodyObject), getMediaType(format)); response = clientRequest.request(getMediaType(format)).post(requestEntity); } catch (Exception e) { throw createExceptionForUnexpectedFailure(clientRequest, e); } try { if (response.getStatus() == Response.Status.CREATED.getStatusCode() || response.getStatus() == Response.Status.OK.getStatusCode()) { return deserialize(response, resultType); } else { throw createExceptionForUnexpectedResponseCode(clientRequest, response); } } finally { response.close(); } }
private <T> T makeGetRequestAndCreateCustomResponse(String uri, Class<T> resultType) { WebTarget clientRequest = httpClient.target(uri); Response response; response = clientRequest.request(getMediaType(format)).get(); try { if (response.getStatus() == Response.Status.OK.getStatusCode()) { return deserialize(response, resultType); } else { throw createExceptionForUnexpectedResponseCode(clientRequest, response); } } finally { response.close(); } }
private void makeDeleteRequest(String uri) { WebTarget clientRequest = httpClient.target(uri); Response response; try { response = clientRequest.request(getMediaType(format)).delete(); } catch (Exception e) { throw createExceptionForUnexpectedFailure(clientRequest, e); } try { if (response.getStatus() != Response.Status.NO_CONTENT.getStatusCode()) { throw createExceptionForUnexpectedResponseCode(clientRequest, response); } } finally { response.close(); } }
spec.addConfig(Capability.RULE, ruleConfig); final String specContent = client.serialize(spec); LOGGER.info("{} content\n{}", marshallingFormat.getType(), specContent); final ContainerSpec specResult = client.deserialize(specContent, ContainerSpec.class);
protected <T> T deserialize(Response response, Class<T> type) { try { if(type == null) { return null; } String content = response.readEntity(String.class); logger.debug("About to deserialize content: \n '{}' \n into type: '{}'", content, type); if (content == null || content.isEmpty()) { return null; } return deserialize(content, type); } catch ( MarshallingException e ) { throw new RuntimeException( "Error while deserializing data received from server!", e ); } }
@Override public ServerTemplate getServerTemplate(String serverTemplateId) { return makeGetRequestAndCreateCustomResponse(controllerBaseUrl + MANAGEMENT_URI_PART + serverTemplateId, ServerTemplate.class); }
@Override public void updateContainerSpec(String serverTemplateId, String containerId, ContainerSpec containerSpec) { makePostRequestAndCreateCustomResponse(controllerBaseUrl + MANAGEMENT_URI_PART + serverTemplateId + CONTAINERS_URI_PART + containerId, containerSpec, Object.class); }
private <T> T makePutRequestAndCreateCustomResponse(String uri, Object bodyObject, Class<T> resultType) { WebTarget clientRequest = httpClient.target(uri); Response response; try { Entity<String> requestEntity = Entity.entity(serialize(bodyObject), getMediaType(format)); response = clientRequest.request(getMediaType(format)).put(requestEntity); } catch (Exception e) { throw createExceptionForUnexpectedFailure(clientRequest, e); } try { if (response.getStatus() == Response.Status.CREATED.getStatusCode()) { return deserialize(response, resultType); } else { throw createExceptionForUnexpectedResponseCode(clientRequest, response); } } finally { response.close(); } }
@Override public ContainerSpec getContainerInfo(String serverTemplateId, String containerId) { return makeGetRequestAndCreateCustomResponse(controllerBaseUrl + MANAGEMENT_URI_PART + serverTemplateId + CONTAINERS_URI_PART + containerId, ContainerSpec.class); }
@Override public void updateContainerConfig(String serverTemplateId, String containerId, Capability capability, ContainerConfig config) { makePostRequestAndCreateCustomResponse(controllerBaseUrl + MANAGEMENT_URI_PART + serverTemplateId + CONTAINERS_URI_PART + containerId + CONFIG_URI_PART + capability.toString(), config, Object.class); }
/** * Creates a new Kie Controller Client using REST based service * @param controllerUrl the URL to the server (e.g.: "http://localhost:8080/kie-server-controller/rest/controller") * @param login user login * @param password user password * @param format marshaling format * @return client instance */ public static KieServerControllerClient newRestClient(final String controllerUrl, final String login, final String password, final MarshallingFormat format) { return new RestKieServerControllerClient(controllerUrl, login, password, format); }
@Override public ServerTemplateList listServerTemplates() { return makeGetRequestAndCreateCustomResponse(controllerBaseUrl + MANAGEMENT_LAST_URI_PART, ServerTemplateList.class); }
@Override public void activateContainer(ContainerSpecKey containerSpecKey) { makePostRequestAndCreateCustomResponse(controllerBaseUrl + MANAGEMENT_URI_PART + containerSpecKey.getServerTemplateKey().getId() + CONTAINERS_URI_PART + containerSpecKey.getId() + ACTIVATED_STATUS_URI_PART, "", null); }
@Override public void deleteContainerSpec(String serverTemplateId, String containerId) { makeDeleteRequest(controllerBaseUrl + MANAGEMENT_URI_PART + serverTemplateId + CONTAINERS_URI_PART + containerId); }
private RuntimeException createExceptionForUnexpectedResponseCode(WebTarget request, Response response) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Unexpected HTTP response code when requesting URI '"); stringBuffer.append(getClientRequestUri(request)); stringBuffer.append("'! Response code: "); stringBuffer.append(response.getStatus()); try { String responseEntity = response.readEntity(String.class); stringBuffer.append(" Response message: "); stringBuffer.append(responseEntity); } catch (IllegalStateException e) { logger.warn("Error trying to read response entity: {}", e.getMessage(), e); } logger.debug(stringBuffer.toString()); return new KieServerControllerHTTPClientException(response.getStatus(), stringBuffer.toString()); }
/** * Creates a new Kie Controller Client using REST based service * @param controllerUrl the URL to the server (e.g.: "http://localhost:8080/kie-server-controller/rest/controller") * @param login user login * @param password user password * @param format marshaling format * @param configuration REST client configuration * @return client instance */ public static KieServerControllerClient newRestClient(final String controllerUrl, final String login, final String password, final MarshallingFormat format, final Configuration configuration) { return new RestKieServerControllerClient(controllerUrl, login, password, format, configuration); }
@Override public ContainerSpecList listContainerSpec(String serverTemplateId) { return makeGetRequestAndCreateCustomResponse(controllerBaseUrl + MANAGEMENT_URI_PART + serverTemplateId + CONTAINERS_LAST_URI_PART, ContainerSpecList.class); }