/** * Issues a registration request for this NiFi instance for the instance at the baseApiUri. * * @param baseApiUri uri to register with * @return response */ public Response issueRegistrationRequest(String baseApiUri) { final URI uri = URI.create(String.format("%s/controller/users", baseApiUri)); // set up the query params MultivaluedHashMap entity = new MultivaluedHashMap(); entity.add("justification", "A Remote instance of NiFi has attempted to create a reference to this NiFi. This action must be approved first."); // get the resource return client.target(uri).request().post(Entity.form(entity)); } }
/** * Performs a POST using the specified url and form data. * * @param uri the uri to post to * @param formData the data to post * @return the client response of the post */ public Response post(URI uri, Map<String, String> formData) { // convert the form data final MultivaluedHashMap<String, String> entity = new MultivaluedHashMap(); for (String key : formData.keySet()) { entity.add(key, formData.get(key)); } // get the resource Invocation.Builder builder = client.target(uri).request().accept(MediaType.APPLICATION_JSON); // get the resource return builder.post(Entity.form(entity)); }
private Optional<SignedJwt> getAndCacheAppTokenFromServer() { MultivaluedMap<String, String> formData = new MultivaluedHashMap<>(); formData.putSingle("grant_type", "client_credentials"); formData.putSingle("scope", "urn:opc:idm:__myscopes__"); Response tokenResponse = tokenEndpoint .request() .accept(MediaType.APPLICATION_JSON_TYPE) .post(Entity.form(formData)); if (tokenResponse.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { JsonObject response = tokenResponse.readEntity(JsonObject.class); String accessToken = response.getString(ACCESS_TOKEN_KEY); LOGGER.finest(() -> "Access token: " + accessToken); SignedJwt signedJwt = SignedJwt.parseToken(accessToken); this.appToken = signedJwt; this.appJwt = signedJwt.getJwt(); return Optional.of(signedJwt); } else { LOGGER.severe("Failed to obtain access token for application to read groups" + " from IDCS. Response code: " + tokenResponse.getStatus() + ", entity: " + tokenResponse.readEntity(String.class)); return Optional.empty(); } }
protected Invocation prepareResource(final String key, final List<String> text, final String sourceLanguage, final String destLanguage) { Invocation.Builder builder = client.target(URL).request(MediaType.APPLICATION_JSON); final MultivaluedHashMap entity = new MultivaluedHashMap(); entity.put("text", text); entity.add("key", key); if ((StringUtils.isBlank(sourceLanguage))) { entity.add("lang", destLanguage); } else { entity.add("lang", sourceLanguage + "-" + destLanguage); } return builder.buildPost(Entity.form(entity)); }
form.param("geolocationRequests", String.valueOf(statistics.getGeolocationRequests())); Context.getClient().target(url).request().async().post(Entity.form(form));
.post(Entity.form(formValues));
.accept(MediaType.APPLICATION_JSON_TYPE) .cacheControl(CacheControl.valueOf("no-cache, no-store, must-revalidate")) .post(Entity.form(formValues));
@Override public IHttpRequest createParamRequest(FhirContext theContext, Map<String, List<String>> theParams, EncodingEnum theEncoding) { MultivaluedMap<String, String> map = new MultivaluedHashMap<String, String>(); for (Map.Entry<String, List<String>> nextParam : theParams.entrySet()) { List<String> value = nextParam.getValue(); for (String s : value) { map.add(nextParam.getKey(), s); } } Entity<Form> entity = Entity.form(map); JaxRsHttpRequest retVal = createHttpRequest(entity); addHeadersToRequest(retVal, theEncoding, theContext); return retVal; }
@Override protected ClientInvocation apply(ClientInvocation invocation, Object object) { Form form = null; Object entity = invocation.getEntity(); if (entity != null) { if (entity instanceof Form) { form = (Form) entity; } else { throw new RuntimeException(Messages.MESSAGES.cannotSetFormParameter()); } } else { form = new Form(); invocation.setEntity(Entity.form(form)); } String value = invocation.getClientConfiguration().toString(object); form.param(paramName, value); return invocation; }
public static <T> T postForm(WebTarget target, MultivaluedMap<String, String> form, MediaType mediaType, Class<T> clazz) { return target.request(mediaType).post(Entity.form(form), clazz); }
public String post( String path, ParameterMap<String, String> params ) { WebTarget webResource = httpClient.target( path ); Response response = webResource.request( MediaType.APPLICATION_JSON_TYPE ).post( Entity.form( convertMap( params ) ) ); return response.readEntity( String.class ); }
public String put( String path, ParameterMap<String, String> params ) { WebTarget webResource = httpClient.target( path ); Response response = webResource.request( MediaType.APPLICATION_JSON_TYPE ).put( Entity.form( convertMap( params ) ) ); return response.readEntity( String.class ); }
protected Invocation createPostWithFullText(final String url, final String fullText) { final Form form = new Form("fullText", fullText); return this.createRequest(url).buildPost(Entity.form(form)); }
private Entity<?> getEntity( Method method, Object[] parameter ) { Entity<?> result = null; if( hasFormParameter( method ) ) { Form form = computeForm( method, parameter ); result = Entity.form( form ); } else if( hasMultiPartFormParameter( method ) ) { MultiPart mp = computeMultiPart( method, parameter ); result = Entity.entity( mp, mp.getMediaType() ); } else { result = determineBodyParameter( method, parameter ); } return result; }
@Test public void testPutBadJobStatus() throws IOException { Response resp = target(String.format("v1/jobs/%s/%s", MockJobProxy.JOB_INSTANCE_2_NAME, MockJobProxy.JOB_INSTANCE_2_ID)) .queryParam("status", "BADSTATUS").request().put(Entity.form(new Form())); assertEquals(400, resp.getStatus()); final Map<String, String> errorMessage = objectMapper.readValue(resp.readEntity(String.class), new TypeReference<Map<String, String>>() {}); assertTrue(errorMessage.get("message").contains("BADSTATUS")); resp.close(); }
@Test public void testPutMissingStatus() throws IOException { Response resp = target(String.format("v1/jobs/%s/%s", MockJobProxy.JOB_INSTANCE_2_NAME, MockJobProxy.JOB_INSTANCE_2_ID)).request() .put(Entity.form(new Form())); assertEquals(400, resp.getStatus()); final Map<String, String> errorMessage = objectMapper.readValue(resp.readEntity(String.class), new TypeReference<Map<String, String>>() {}); assertTrue(errorMessage.get("message").contains("status")); resp.close(); }
@Test public void testInterceptorInvokedOnFormAndFormParamMatchesFormValue() throws Exception { Client client = ClientBuilder.newClient(); String uri = "http://localhost:" + PORT + "/form"; Form f = new Form("value", "ORIGINAL"); Response r = client.target(uri) .request(MediaType.APPLICATION_FORM_URLENCODED) .post(Entity.form(f)); Assert.assertEquals("MODIFIED", r.getHeaderString("FromForm")); Assert.assertEquals("MODIFIED", r.getHeaderString("FromFormParam")); } }
@Test public void testStopJob() throws IOException { Response resp = target(String.format("v1/jobs/%s/%s", MockJobProxy.JOB_INSTANCE_2_NAME, MockJobProxy.JOB_INSTANCE_2_ID)) .queryParam("status", "stopped").request().put(Entity.form(new Form())); assertEquals(202, resp.getStatus()); final Job job2 = objectMapper.readValue(resp.readEntity(String.class), Job.class); assertEquals(MockJobProxy.JOB_INSTANCE_2_NAME, job2.getJobName()); assertEquals(MockJobProxy.JOB_INSTANCE_2_ID, job2.getJobId()); assertStatusNotDefault(job2); resp.close(); }
@Test public void testStartJob() throws IOException { Response resp = target(String.format("v1/jobs/%s/%s", MockJobProxy.JOB_INSTANCE_2_NAME, MockJobProxy.JOB_INSTANCE_2_ID)) .queryParam("status", "started").request().put(Entity.form(new Form())); assertEquals(202, resp.getStatus()); final Job job2 = objectMapper.readValue(resp.readEntity(String.class), Job.class); assertEquals(MockJobProxy.JOB_INSTANCE_2_NAME, job2.getJobName()); assertEquals(MockJobProxy.JOB_INSTANCE_2_ID, job2.getJobId()); assertStatusNotDefault(job2); resp.close(); }
void rememberMeUserSession() { WebTarget sessionResource = webTarget("session"); Form form = new Form() .param("username", USER1) .param("password", USER1_PASSWORD) .param("rememberMe", "true"); Response response = sessionResource.request().post(Entity.form(form)); int loginStatus = response.getStatus(); response.close(); assertEquals(200, loginStatus); }