List of usage examples for org.apache.http.entity StringEntity setContentType
public void setContentType(Header header)
From source file:net.maritimecloud.identityregistry.utils.KeycloakServiceAccountUtil.java
public void createUser(String name, String password, String email, String orgShortName) { UserRepresentation user = new UserRepresentation(); user.setUsername(name);/* w ww. j av a2 s . com*/ user.setEnabled(true); if (email != null && !email.trim().isEmpty()) { user.setEmail(email); user.setEmailVerified(true); } // Set roles for the MD identity register - the clientId of the portal should be updated as needed! /*HashMap<String, List<String>> clientRoles = new HashMap<String, List<String>>(); String clientId = "mcregportal"; //deployment.getResourceCredentials().keySet().iterator().next(); clientRoles.put(clientId, Arrays.asList("ROLE_ADMIN")); user.setClientRoles(clientRoles);*/ // Set attributes Map<String, Object> attr = new HashMap<String, Object>(); attr.put("org", orgShortName); attr.put("permissions", "MCADMIN"); user.setAttributes(attr); // Set credentials CredentialRepresentation cred = new CredentialRepresentation(); cred.setType(CredentialRepresentation.PASSWORD); cred.setValue(password); user.setCredentials(Arrays.asList(cred)); // Now POST the IDP data to keycloak CloseableHttpClient client = HttpClientBuilder.create().build(); try { HttpPost post = new HttpPost(deployment.getAuthServerBaseUrl() + "/admin/realms/" + realm + "/users"); if (token != null) { post.addHeader("Authorization", "Bearer " + token); } System.out.println("user creating json: " + JsonSerialization.writeValueAsString(user)); StringEntity input = new StringEntity(JsonSerialization.writeValueAsString(user)); input.setContentType("application/json"); post.setEntity(input); HttpResponse response = client.execute(post); int status = response.getStatusLine().getStatusCode(); HttpEntity entity = response.getEntity(); if (status != 201) { String json = getContent(entity); String error = "IDP creation failed. Bad status: " + status + " response: " + json; System.out.println(error); //req.setAttribute(ERROR, error); } else { System.out.println("IDP created! " + getContent(entity)); } } catch (IOException ioe) { ioe.printStackTrace(); } }
From source file:com.biomeris.i2b2.export.ws.ExportService.java
private boolean validateUser(Network network) throws JAXBException, IOException, TransformerException { boolean output = false; String pmRequest = MessageBuilder.buildPMGetServiceRequest(network); CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(network.getProxyAddress()); StringEntity entity1A = new StringEntity(pmRequest, Consts.UTF_8); entity1A.setContentType("text/xml"); entity1A.setChunked(true);//www.ja v a 2s . c o m httpPost.setEntity(entity1A); CloseableHttpResponse response = null; try { response = httpclient.execute(httpPost); log.debug("Proxy: " + network.getProxyAddress()); } catch (HttpHostConnectException hhce) { try { if (network.getStaticProxyAddress() != null) { httpPost = new HttpPost(network.getStaticProxyAddress()); httpPost.setEntity(entity1A); response = httpclient.execute(httpPost); log.debug("Proxy: " + network.getStaticProxyAddress()); } } catch (HttpHostConnectException e) { throw e; } } try { HttpEntity entity1R = response.getEntity(); String responseString = EntityUtils.toString(entity1R); ResponseMessageType rmt = JAXB.unmarshal(new StringReader(responseString), ResponseMessageType.class); ResponseHeaderType responseHeader = rmt.getResponseHeader(); ResultStatusType responseStatus = responseHeader.getResultStatus(); if (responseStatus.getStatus().getType().equals("ERROR")) { return false; } BodyType body = rmt.getMessageBody(); ElementNSImpl bodyEl = (ElementNSImpl) body.getAny().get(0); String bodyStr = elementToString(bodyEl); ConfigureType configureType = JAXB.unmarshal(new StringReader(bodyStr), ConfigureType.class); UserType userType = configureType.getUser(); if (userType.isIsAdmin()) { output = true; } else { List<ProjectType> userProjects = userType.getProject(); for (ProjectType pt : userProjects) { if (pt.getId().equals(network.getProject())) { if (userAccessLevel(pt.getRole()) >= authLevel) { output = true; } } } } EntityUtils.consume(entity1R); } finally { response.close(); } return output; }
From source file:com.marklogic.client.example.util.Bootstrapper.java
/** * Programmatic invocation./*from w ww . j av a 2 s .c o m*/ * @param configServer the configuration server for creating the REST server * @param restServer the specification of the REST server */ public void makeServer(ConfigServer configServer, RESTServer restServer) throws ClientProtocolException, IOException, FactoryConfigurationError { DefaultHttpClient client = new DefaultHttpClient(); String host = configServer.getHost(); int configPort = configServer.getPort(); // TODO: SSL Authentication authType = configServer.getAuthType(); if (authType != null) { List<String> prefList = new ArrayList<String>(); if (authType == Authentication.BASIC) prefList.add(AuthPolicy.BASIC); else if (authType == Authentication.DIGEST) prefList.add(AuthPolicy.DIGEST); else throw new IllegalArgumentException("Unknown authentication type: " + authType.name()); client.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, prefList); String configUser = configServer.getUser(); String configPassword = configServer.getPassword(); client.getCredentialsProvider().setCredentials(new AuthScope(host, configPort), new UsernamePasswordCredentials(configUser, configPassword)); } BasicHttpContext context = new BasicHttpContext(); StringEntity content; try { content = new StringEntity(restServer.toXMLString()); } catch (XMLStreamException e) { throw new IOException("Could not create payload to bootstrap server."); } content.setContentType("application/xml"); HttpPost poster = new HttpPost("http://" + host + ":" + configPort + "/v1/rest-apis"); poster.setEntity(content); HttpResponse response = client.execute(poster, context); //poster.releaseConnection(); StatusLine status = response.getStatusLine(); int statusCode = status.getStatusCode(); String statusPhrase = status.getReasonPhrase(); client.getConnectionManager().shutdown(); if (statusCode >= 300) { throw new RuntimeException("Failed to create REST server: " + statusCode + " " + statusPhrase + "\n" + "Please check the server log for detail"); } }
From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java
/** * Delete user in clean up above if the test user exists * @throws Exception/*from ww w . j a v a2 s . c o m*/ */ private void delUser() throws Exception { // getUser and check status HttpPost httpPost = new HttpPost("http://example:8080/api/rs"); StringEntity input = new StringEntity(delUser); input.setContentType("application/json"); httpPost.setEntity(input); httpPost.setHeader("Authorization", ownerToken); CloseableHttpResponse response = httpclient.execute(httpPost); try { assertEquals(200, response.getStatusLine().getStatusCode()); HttpEntity entity = response.getEntity(); BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent())); String json = ""; String line = ""; while ((line = rd.readLine()) != null) { json = json + line; } // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity); } finally { response.close(); } }
From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java
private void postUpdForm() throws Exception { HttpPost httpPost = new HttpPost("http://example:8080/api/rs"); StringEntity input = new StringEntity(updJson); input.setContentType("application/json"); httpPost.setEntity(input);// w w w.j a v a2 s . c o m CloseableHttpResponse response = httpclient.execute(httpPost); try { System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent())); String json = ""; String line = ""; while ((line = rd.readLine()) != null) { json = json + line; } System.out.println("json = " + json); assertEquals("{\"data\":\"success\"}", json); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity); } finally { response.close(); } }
From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java
private void postAddForm() throws Exception { HttpPost httpPost = new HttpPost("http://example:8080/api/rs"); StringEntity input = new StringEntity(addJson); input.setContentType("application/json"); httpPost.setEntity(input);/* w ww . ja v a 2 s .c o m*/ CloseableHttpResponse response = httpclient.execute(httpPost); try { System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent())); String json = ""; String line = ""; while ((line = rd.readLine()) != null) { json = json + line; } System.out.println("json = " + json); assertEquals("{\"data\":\"success\"}", json); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity); } finally { response.close(); } }
From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java
private void postDelForm() throws Exception { System.out.println("postDelForm starts"); HttpPost httpPost = new HttpPost("http://example:8080/api/rs"); StringEntity input = new StringEntity(delJson); input.setContentType("application/json"); httpPost.setEntity(input);//from w w w . j av a 2 s . co m CloseableHttpResponse response = httpclient.execute(httpPost); try { System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent())); String json = ""; String line = ""; while ((line = rd.readLine()) != null) { json = json + line; } System.out.println("json = " + json); assertEquals("{\"data\":\"success\"}", json); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity); } finally { response.close(); } System.out.println("postDelForm ends"); }
From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java
/** * SignUp a new test user/* w ww . j a va 2s.c o m*/ * @throws Exception */ private void signUpUser() throws Exception { // getUser and check status HttpPost httpPost = new HttpPost("http://example:8080/api/rs"); StringEntity input = new StringEntity(signUpJson); input.setContentType("application/json"); httpPost.setEntity(input); CloseableHttpResponse response = httpclient.execute(httpPost); try { assertEquals(200, response.getStatusLine().getStatusCode()); HttpEntity entity = response.getEntity(); BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent())); String json = ""; String line = ""; while ((line = rd.readLine()) != null) { json = json + line; } // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity); } finally { response.close(); } }
From source file:org.clinical3po.backendservices.server.handler.RestHandlerTest.java
private void postGetAllForm() throws Exception { HttpPost httpPost = new HttpPost("http://example:8080/api/rs"); StringEntity input = new StringEntity(getAllJson); input.setContentType("application/json"); httpPost.setEntity(input);//w w w .j av a2 s. co m CloseableHttpResponse response = httpclient.execute(httpPost); try { System.out.println(response.getStatusLine()); HttpEntity entity = response.getEntity(); BufferedReader rd = new BufferedReader(new InputStreamReader(entity.getContent())); String json = ""; String line = ""; while ((line = rd.readLine()) != null) { json = json + line; } System.out.println("json = " + json); // do something useful with the response body // and ensure it is fully consumed EntityUtils.consume(entity); } finally { response.close(); } }