public static String getAuthorizationCode(WebClient client, String scope) { // Make initial authorization request client.type("application/json").accept("application/json"); client.query("client_id", "consumer-id"); client.query("redirect_uri", "http://www.blah.apache.org"); client.query("response_type", "code"); if (scope != null) { client.query("scope", scope); } client.path("authorize/"); Response response = client.get(); OAuthAuthorizationData authzData = response.readEntity(OAuthAuthorizationData.class); // Now call "decision" to get the authorization code grant client.path("decision"); client.type("application/x-www-form-urlencoded"); Form form = new Form(); form.param("session_authenticity_token", authzData.getAuthenticityToken()); form.param("client_id", authzData.getClientId()); form.param("redirect_uri", authzData.getRedirectUri()); if (authzData.getProposedScope() != null) { form.param("scope", authzData.getProposedScope()); } form.param("oauthDecision", "allow"); response = client.post(form); String location = response.getHeaderString("Location"); return getSubstring(location, "code"); }
private String lookupObject(final String contextKey) { WebClient client = getWebClient() .accept(MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON) .path(CALL_PATH, contextKey); String object = null; try { object = client.get(String.class); } catch (NotFoundException e) { return null; } catch (WebApplicationException e) { handleWebException(e); } catch (Throwable e) { if (e instanceof ConnectException || e instanceof ClientException) { if (null != client) { client.reset(); } switchServerURL(client.getBaseURI().toString()); return lookupObject(contextKey); } } finally { if (null != client) { client.reset(); } } return object; }
public void testPie1() { try { String url = baseURL + "/rest/Pie/search;id=*/utensilCollection"; WebClient client = WebClient.create(url); client.type("application/xml").accept("application/xml"); Response response = client.get(); if (response.getStatus() == Status.NOT_ACCEPTABLE.getStatusCode()) { InputStream is = (InputStream) response.getEntity(); org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(false); org.jdom.Document jDoc = builder.build(is); assertEquals(jDoc.getRootElement().getName(), "response"); } else if (response.getStatus() == Status.NOT_FOUND.getStatusCode()) { InputStream is = (InputStream) response.getEntity(); org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(false); org.jdom.Document jDoc = builder.build(is); assertEquals(jDoc.getRootElement().getName(), "response"); } else if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } File myFile = new File("Pie_Search" + "XML.xml"); System.out.println("writing data to file " + myFile.getAbsolutePath()); FileWriter myWriter = new FileWriter(myFile); BufferedReader br = new BufferedReader(new InputStreamReader(((InputStream) response.getEntity()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { myWriter.write(output); System.out.println(output); } myWriter.flush(); myWriter.close(); } catch (Exception e) { e.printStackTrace(); ResponseBuilder builder = Response.status(Status.INTERNAL_SERVER_ERROR); builder.type("application/xml"); StringBuffer buffer = new StringBuffer(); buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); buffer.append("<response>"); buffer.append("<type>ERROR</type>"); buffer.append("<code>INTERNAL_ERROR_4</code>"); buffer.append("<message>Failed to Query due to: " + e.getMessage() + "</message>"); buffer.append("</response>"); builder.entity(buffer.toString()); throw new WebApplicationException(builder.build()); } }
public void showDocumentUpload() { LOGGER.info("Class :: " + this.getClass() + " :: Method :: showDocumentUpload"); if (getDocIds().size() != 0) { for (String doc : getDocIds()) { WebClient client = IclubWebHelper.createCustomClient(D_BASE_URL + "del/" + doc); client.get(); client.close(); } } getDocIds().clear(); }
public void deleteDocument(String selDocId) { try { WebClient client = IclubWebHelper.createCustomClient(D_BASE_URL + "del/" + selDocId); client.get(); client.close(); } catch (Exception e) { LOGGER.error(e, e); IclubWebHelper.addMessage( getLabelBundle().getString("doucmentuploadingerror") + " :: " + e.getMessage(), FacesMessage.SEVERITY_ERROR); } }
public void handleMessage(Message message) throws Fault { SecurityContext context = message.get(SecurityContext.class); if (context == null) { return; } Principal principal = context.getUserPrincipal(); UsernameToken usernameToken = (UsernameToken) message.get(SecurityToken.class); if (principal == null || usernameToken == null || !principal.getName().equals(usernameToken.getName())) { return; } // Read the user from Syncope and get the roles WebClient client = WebClient.create(address, Collections.singletonList(new JacksonJsonProvider())); String authorizationHeader = "Basic " + Base64Utility.encode( (usernameToken.getName() + ":" + usernameToken.getPassword()).getBytes()); client.header("Authorization", authorizationHeader); client = client.path("users/self"); UserTO user = null; try { user = client.get(UserTO.class); if (user == null) { Exception exception = new Exception("Authentication failed"); throw new Fault(exception); } } catch (RuntimeException ex) { if (log.isDebugEnabled()) { log.debug(ex.getMessage(), ex); } throw new Fault(ex); } // Now get the roles List<MembershipTO> membershipList = user.getMemberships(); Subject subject = new Subject(); subject.getPrincipals().add(principal); for (MembershipTO membership : membershipList) { String roleName = membership.getRoleName(); subject.getPrincipals().add(new SimpleGroup(roleName, usernameToken.getName())); } subject.setReadOnly(); message.put(SecurityContext.class, new DefaultSecurityContext(principal, subject)); }
// Optional step - may be needed to ensure that by the time individual // tests start running the endpoint has been fully initialized private static void waitForWADL() throws InterruptedException { LOGGER.info("Waiting for wadl"); WebClient client = WebClient.create(WADL_ADDRESS); // wait for 20 secs or so for (int i = 0; i < 20; i++) { Thread.currentThread().sleep(200); Response response = client.get(); if (response.getStatus() == 200) { break; } } // no WADL is available yet - throw an exception or give tests a chance // to run anyway }
@Test @Ignore public void testApiListingIsProperlyReturnedJSON() throws Exception { final WebClient client = createWebClient("/swagger.json"); try { final Response r = client.get(); assertEquals(Status.OK.getStatusCode(), r.getStatus()); JSONAssert.assertEquals( getExpectedValue(getExpectedFileJson(), getPort()), IOUtils.readStringFromStream((InputStream) r.getEntity()), false); } finally { client.close(); } }
@Test @Ignore public void statuslist() throws Exception { WebClient wc = WebClient.create( "http://localhost:8080/forsendelse/service/rest/forsendelsesservice/statuslist?ids=000000001689.pdf,000000001690.pdf"); wc.type("text/xml"); XMLSource source = wc.get(XMLSource.class); source.setBuffering(true); ForsendelseStatusRest b1 = source.getNode("/books/book[position() = 1]", ForsendelseStatusRest.class); assertNotNull(b1); ForsendelseStatusRest b2 = source.getNode("/books/book[position() = 2]", ForsendelseStatusRest.class); assertNotNull(b2); }
public void testSearch() throws Exception { try { String url = baseURL + "/rest/Pupil/search;id=*"; WebClient client = WebClient.create(url); client.type("application/xml").accept("application/xml"); Response response = client.get(); if (response.getStatus() == Status.NOT_ACCEPTABLE.getStatusCode()) { InputStream is = (InputStream) response.getEntity(); org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(false); org.jdom.Document jDoc = builder.build(is); assertEquals(jDoc.getRootElement().getName(), "response"); } else if (response.getStatus() == Status.NOT_FOUND.getStatusCode()) { InputStream is = (InputStream) response.getEntity(); org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(false); org.jdom.Document jDoc = builder.build(is); assertEquals(jDoc.getRootElement().getName(), "response"); } else if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } File myFile = new File("Pupil_Search" + "XML.xml"); System.out.println("writing data to file " + myFile.getAbsolutePath()); FileWriter myWriter = new FileWriter(myFile); BufferedReader br = new BufferedReader(new InputStreamReader(((InputStream) response.getEntity()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { myWriter.write(output); System.out.println(output); } myWriter.flush(); myWriter.close(); } catch (Exception e) { e.printStackTrace(); } }
@Test @Ignore public void testApiListingIsProperlyReturnedYAML() throws Exception { final WebClient client = createWebClient("/swagger.yaml"); try { final Response r = client.get(); assertEquals(Status.OK.getStatusCode(), r.getStatus()); // REVISIT find a better way of reliably comparing two yaml instances. // I noticed that yaml.load instantiates a Map and // for an integer valued key, an Integer or a String is arbitrarily instantiated, // which leads to the assertion error. So, we serilialize the yamls and compare the // re-serialized texts. Yaml yaml = new Yaml(); assertEquals( yaml.load(getExpectedValue(getExpectedFileYaml(), getPort())).toString(), yaml.load(IOUtils.readStringFromStream((InputStream) r.getEntity())).toString()); } finally { client.close(); } }
/** * Uses Nested Search Criteria for search Verifies that the results are returned Verifies size of * the result set Verifies that none of the attributes are null * * @throws Exception */ public void testGet() throws Exception { try { Pupil searchObject = new Pupil(); Collection results = getApplicationService() .search("gov.nih.nci.cacoresdk.domain.inheritance.abstrakt.Pupil", searchObject); String id = ""; if (results != null && results.size() > 0) { Pupil obj = (Pupil) ((List) results).get(0); id = obj.getId().getExtension(); } else return; if (id.equals("")) return; String url = baseURL + "/rest/Pupil/" + id; WebClient client = WebClient.create(url); client.type("application/xml").accept("application/xml"); Response response = client.get(); if (response.getStatus() == Status.NOT_ACCEPTABLE.getStatusCode()) { InputStream is = (InputStream) response.getEntity(); org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(false); org.jdom.Document jDoc = builder.build(is); assertEquals(jDoc.getRootElement().getName(), "response"); } else if (response.getStatus() == Status.NOT_FOUND.getStatusCode()) { InputStream is = (InputStream) response.getEntity(); org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(false); org.jdom.Document jDoc = builder.build(is); assertEquals(jDoc.getRootElement().getName(), "response"); } else if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } File myFile = new File("Pupil" + "XML.xml"); System.out.println("writing data to file " + myFile.getAbsolutePath()); FileWriter myWriter = new FileWriter(myFile); BufferedReader br = new BufferedReader(new InputStreamReader(((InputStream) response.getEntity()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { myWriter.write(output); System.out.println(output); } myWriter.flush(); myWriter.close(); } catch (Exception e) { e.printStackTrace(); throw e; } }
public void testgetUtensilCollection() { try { Pie searchObject = new Pie(); Collection results4 = getApplicationService() .search( "gov.nih.nci.cacoresdk.domain.other.differentpackage.associations.Pie", searchObject); String id = ""; if (results4 != null && results4.size() > 0) { Pie obj = (Pie) ((List) results4).get(0); Integer idVal = obj.getId(); id = new Integer(idVal).toString(); } else return; if (id.equals("")) return; String url = baseURL + "/rest/Pie/" + id + "/utensilCollection"; WebClient client = WebClient.create(url); client.type("application/xml").accept("application/xml"); Response response = client.get(); if (response.getStatus() == Status.NOT_ACCEPTABLE.getStatusCode()) { InputStream is = (InputStream) response.getEntity(); org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(false); org.jdom.Document jDoc = builder.build(is); assertEquals(jDoc.getRootElement().getName(), "response"); } else if (response.getStatus() == Status.NOT_FOUND.getStatusCode()) { InputStream is = (InputStream) response.getEntity(); org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder(false); org.jdom.Document jDoc = builder.build(is); assertEquals(jDoc.getRootElement().getName(), "response"); } else if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } File myFile = new File("Pie_Search" + "XML.xml"); System.out.println("writing data to file " + myFile.getAbsolutePath()); FileWriter myWriter = new FileWriter(myFile); BufferedReader br = new BufferedReader(new InputStreamReader(((InputStream) response.getEntity()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { myWriter.write(output); System.out.println(output); } myWriter.flush(); myWriter.close(); } catch (Exception e) { e.printStackTrace(); ResponseBuilder builder = Response.status(Status.INTERNAL_SERVER_ERROR); builder.type("application/xml"); StringBuffer buffer = new StringBuffer(); buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); buffer.append("<response>"); buffer.append("<type>ERROR</type>"); buffer.append("<code>INTERNAL_ERROR_4</code>"); buffer.append("<message>Failed to Query due to: " + e.getMessage() + "</message>"); buffer.append("</response>"); builder.entity(buffer.toString()); throw new WebApplicationException(builder.build()); } }