List of usage examples for org.springframework.http HttpMethod GET
HttpMethod GET
To view the source code for org.springframework.http HttpMethod GET.
Click Source Link
From source file:sample.RestTests.java
private <T> ResponseEntity<T> getForUser(String resourceUrl, HttpHeaders headers, Class<T> type) { return this.restTemplate.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<T>(headers), type); }
From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.FetchDiseases.java
/** * Method, where all diseases are read from server. * All heavy lifting is made here.//from w ww .jav a 2 s . c o m * * @param params omitted here * @return list of fetched diseases */ @Override protected List<Disease> doInBackground(Void... params) { SharedPreferences credentials = getCredentials(); String username = credentials.getString("username", null); String password = credentials.getString("password", null); String url = credentials.getString("url", null) + Values.SERVICE_DISEASES; setState(RUNNING, R.string.working_ws_disease); HttpAuthentication authHeader = new HttpBasicAuthentication(username, password); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAuthorization(authHeader); requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML)); HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders); SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory(); // Create a new RestTemplate instance RestTemplate restTemplate = new RestTemplate(factory); restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter()); try { // Make the network request Log.d(TAG, url); ResponseEntity<DiseaseList> response = restTemplate.exchange(url, HttpMethod.GET, entity, DiseaseList.class); DiseaseList body = response.getBody(); if (body != null) { return body.getDiseases(); } } catch (Exception e) { Log.e(TAG, e.getLocalizedMessage(), e); setState(ERROR, e); } finally { setState(DONE); } return Collections.emptyList(); }
From source file:com.blogspot.sgdev.blog.GrantByAuthorizationCodeProviderTest.java
@Test public void getJwtTokenByAuthorizationCode() throws JsonParseException, JsonMappingException, IOException, URISyntaxException { String redirectUrl = "http://localhost:" + port + "/resources/user"; ResponseEntity<String> response = new TestRestTemplate("user", "password").postForEntity( "http://localhost:" + port + "/oauth/authorize?response_type=code&client_id=normal-app&redirect_uri={redirectUrl}", null, String.class, redirectUrl); assertEquals(HttpStatus.OK, response.getStatusCode()); List<String> setCookie = response.getHeaders().get("Set-Cookie"); String jSessionIdCookie = setCookie.get(0); String cookieValue = jSessionIdCookie.split(";")[0]; HttpHeaders headers = new HttpHeaders(); headers.add("Cookie", cookieValue); response = new TestRestTemplate("user", "password").postForEntity("http://localhost:" + port + "oauth/authorize?response_type=code&client_id=normal-app&redirect_uri={redirectUrl}&user_oauth_approval=true&authorize=Authorize", new HttpEntity<Void>(headers), String.class, redirectUrl); assertEquals(HttpStatus.FOUND, response.getStatusCode()); assertNull(response.getBody());/*from www . j a va2 s .c o m*/ String location = response.getHeaders().get("Location").get(0); URI locationURI = new URI(location); String query = locationURI.getQuery(); location = "http://localhost:" + port + "/oauth/token?" + query + "&grant_type=authorization_code&client_id=normal-app&redirect_uri={redirectUrl}"; response = new TestRestTemplate("normal-app", "").postForEntity(location, new HttpEntity<Void>(new HttpHeaders()), String.class, redirectUrl); assertEquals(HttpStatus.OK, response.getStatusCode()); HashMap jwtMap = new ObjectMapper().readValue(response.getBody(), HashMap.class); String accessToken = (String) jwtMap.get("access_token"); headers = new HttpHeaders(); headers.set("Authorization", "Bearer " + accessToken); response = new TestRestTemplate().exchange("http://localhost:" + port + "/resources/client", HttpMethod.GET, new HttpEntity<String>(null, headers), String.class); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); response = new TestRestTemplate().exchange("http://localhost:" + port + "/resources/user", HttpMethod.GET, new HttpEntity<String>(null, headers), String.class); assertEquals(HttpStatus.OK, response.getStatusCode()); response = new TestRestTemplate().exchange("http://localhost:" + port + "/resources/principal", HttpMethod.GET, new HttpEntity<String>(null, headers), String.class); assertEquals("user", response.getBody()); response = new TestRestTemplate().exchange("http://localhost:" + port + "/resources/roles", HttpMethod.GET, new HttpEntity<String>(null, headers), String.class); assertEquals("[{\"authority\":\"ROLE_USER\"}]", response.getBody()); }
From source file:com.cloud.baremetal.networkservice.Force10BaremetalSwitchBackend.java
@Override public void prepareVlan(BaremetalVlanStruct struct) { String link = buildLink(struct.getSwitchIp(), String.format("/api/running/ftos/interface/vlan/%s", struct.getVlan())); HttpHeaders headers = createBasicAuthenticationHeader(struct); HttpEntity<String> request = new HttpEntity<>(headers); ResponseEntity rsp = rest.exchange(link, HttpMethod.GET, request, String.class); logger.debug(String.format("http get: %s", link)); if (rsp.getStatusCode() == HttpStatus.NOT_FOUND) { PortInfo port = new PortInfo(struct); XmlObject xml = new XmlObject("vlan") .putElement("vlan-id", new XmlObject("vlan-id").setText(String.valueOf(struct.getVlan()))) .putElement("untagged", new XmlObject("untagged").putElement(port.interfaceType, new XmlObject(port.interfaceType).putElement("name", new XmlObject("name").setText(port.port)))) .putElement("shutdown", new XmlObject("shutdown").setText("false")); request = new HttpEntity<>(xml.dump(), headers); link = buildLink(struct.getSwitchIp(), String.format("/api/running/ftos/interface/")); logger.debug(String.format("http get: %s, body: %s", link, request)); rsp = rest.exchange(link, HttpMethod.POST, request, String.class); if (!successHttpStatusCode.contains(rsp.getStatusCode())) { throw new CloudRuntimeException(String.format( "unable to create vlan[%s] on force10 switch[ip:%s]. HTTP status code:%s, body dump:%s", struct.getVlan(), struct.getSwitchIp(), rsp.getStatusCode(), rsp.getBody())); } else {/* w w w . j a va2 s. c o m*/ logger.debug(String.format( "successfully programmed vlan[%s] on Force10[ip:%s, port:%s]. http response[status code:%s, body:%s]", struct.getVlan(), struct.getSwitchIp(), struct.getPort(), rsp.getStatusCode(), rsp.getBody())); } } else if (successHttpStatusCode.contains(rsp.getStatusCode())) { PortInfo port = new PortInfo(struct); XmlObject xml = XmlObjectParser.parseFromString((String) rsp.getBody()); List<XmlObject> ports = xml.getAsList("untagged.tengigabitethernet"); ports.addAll(xml.<XmlObject>getAsList("untagged.gigabitethernet")); ports.addAll(xml.<XmlObject>getAsList("untagged.fortyGigE")); for (XmlObject pxml : ports) { XmlObject name = pxml.get("name"); if (port.port.equals(name.getText())) { logger.debug(String.format("port[%s] has joined in vlan[%s], no need to program again", struct.getPort(), struct.getVlan())); return; } } xml.removeElement("mtu"); xml.setText(null); XmlObject tag = xml.get("untagged"); if (tag == null) { tag = new XmlObject("untagged"); xml.putElement("untagged", tag); } tag.putElement(port.interfaceType, new XmlObject(port.interfaceType).putElement("name", new XmlObject("name").setText(port.port))); request = new HttpEntity<>(xml.dump(), headers); link = buildLink(struct.getSwitchIp(), String.format("/api/running/ftos/interface/vlan/%s", struct.getVlan())); logger.debug(String.format("http get: %s, body: %s", link, request)); rsp = rest.exchange(link, HttpMethod.PUT, request, String.class); if (!successHttpStatusCode.contains(rsp.getStatusCode())) { throw new CloudRuntimeException(String.format( "failed to program vlan[%s] for port[%s] on force10[ip:%s]. http status:%s, body dump:%s", struct.getVlan(), struct.getPort(), struct.getSwitchIp(), rsp.getStatusCode(), rsp.getBody())); } else { logger.debug(String.format( "successfully join port[%s] into vlan[%s] on Force10[ip:%s]. http response[status code:%s, body:%s]", struct.getPort(), struct.getVlan(), struct.getSwitchIp(), rsp.getStatusCode(), rsp.getBody())); } } else { throw new CloudRuntimeException( String.format("force10[ip:%s] returns unexpected error[%s] when http getting %s, body dump:%s", struct.getSwitchIp(), rsp.getStatusCode(), link, rsp.getBody())); } }
From source file:com.mycompany.CPUTAuction.restapi.BidRestControllerTest.java
@Test public void testgetAllBids() { HttpEntity<?> requestEntity = getHttpEntity(); ResponseEntity<Bid[]> responseEntity = restTemplate.exchange(URL + "api/bid/bids", HttpMethod.GET, requestEntity, Bid[].class); Bid[] bids = responseEntity.getBody(); for (Bid bid : bids) { System.out.println("The Bid id is " + bid.getBidID()); }/*from w w w . ja v a2s. c o m*/ Assert.assertTrue(bids.length > 0); }
From source file:com.cloudera.nav.sdk.client.NavApiCient.java
/** * Call the Navigator API and retrieve all available sources * * @return a collection of available sources *//*from ww w. j ava 2 s . c om*/ public Collection<Source> getAllSources() { String url = entitiesQueryUrl(); SourceAttrs[] sourceAttrs = sendRequest(url, HttpMethod.GET, SourceAttrs[].class); Collection<Source> sources = Lists.newArrayListWithExpectedSize(sourceAttrs.length + 1); for (SourceAttrs info : sourceAttrs) { sources.add(info.createSource()); } return sources; }
From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.FetchArtifacts.java
/** * Method, where all artifacts are read from server. * All heavy lifting is made here.// ww w . jav a2s . c om * * @param params omitted here * @return list of fetched artifacts */ @Override protected List<Artifact> doInBackground(Void... params) { SharedPreferences credentials = getCredentials(); String username = credentials.getString("username", null); String password = credentials.getString("password", null); String url = credentials.getString("url", null) + Values.SERVICE_ARTIFACTS; setState(RUNNING, R.string.working_ws_artifacts); HttpAuthentication authHeader = new HttpBasicAuthentication(username, password); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAuthorization(authHeader); requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML)); HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders); SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory(); // Create a new RestTemplate instance RestTemplate restTemplate = new RestTemplate(factory); restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter()); try { // Make the network request Log.d(TAG, url); ResponseEntity<ArtifactList> response = restTemplate.exchange(url, HttpMethod.GET, entity, ArtifactList.class); ArtifactList body = response.getBody(); if (body != null) { return body.getArtifacts(); } } catch (Exception e) { Log.e(TAG, e.getLocalizedMessage(), e); setState(ERROR, e); } finally { setState(DONE); } return Collections.emptyList(); }
From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.FetchHardwareList.java
/** * Method, where all hardwareList are read from server. * All heavy lifting is made here./*from w ww .j a va2s. c o m*/ * * @param params omitted here * @return list of fetched hardware */ @Override protected List<Hardware> doInBackground(Void... params) { SharedPreferences credentials = getCredentials(); String username = credentials.getString("username", null); String password = credentials.getString("password", null); String url = credentials.getString("url", null) + Values.SERVICE_HARDWARE; setState(RUNNING, R.string.working_ws_hardware); HttpAuthentication authHeader = new HttpBasicAuthentication(username, password); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAuthorization(authHeader); requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML)); HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders); SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory(); // Create a new RestTemplate instance RestTemplate restTemplate = new RestTemplate(factory); restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter()); try { // Make the network request Log.d(TAG, url); ResponseEntity<HardwareList> response = restTemplate.exchange(url, HttpMethod.GET, entity, HardwareList.class); HardwareList body = response.getBody(); if (body != null) { return body.getHardwareList(); } } catch (Exception e) { Log.e(TAG, e.getLocalizedMessage(), e); setState(ERROR, e); } finally { setState(DONE); } return Collections.emptyList(); }
From source file:cz.zcu.kiv.eeg.mobile.base.ws.asynctask.FetchSoftwareList.java
/** * Method, where all softwareList are read from server. * All heavy lifting is made here.//from w w w .j a v a 2 s . c o m * * @param params omitted here * @return list of fetched software */ @Override protected List<Software> doInBackground(Void... params) { SharedPreferences credentials = getCredentials(); String username = credentials.getString("username", null); String password = credentials.getString("password", null); String url = credentials.getString("url", null) + Values.SERVICE_SOFTWARE; setState(RUNNING, R.string.working_ws_software); HttpAuthentication authHeader = new HttpBasicAuthentication(username, password); HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setAuthorization(authHeader); requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_XML)); HttpEntity<Object> entity = new HttpEntity<Object>(requestHeaders); SSLSimpleClientHttpRequestFactory factory = new SSLSimpleClientHttpRequestFactory(); // Create a new RestTemplate instance RestTemplate restTemplate = new RestTemplate(factory); restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter()); try { // Make the network request Log.d(TAG, url); ResponseEntity<SoftwareList> response = restTemplate.exchange(url, HttpMethod.GET, entity, SoftwareList.class); SoftwareList body = response.getBody(); if (body != null) { return body.getSoftwareList(); } } catch (Exception e) { Log.e(TAG, e.getLocalizedMessage(), e); setState(ERROR, e); } finally { setState(DONE); } return Collections.emptyList(); }
From source file:com.bailen.radioOnline.recursos.REJA.java
public Cancion[] getRatings(String apiKey) throws IOException { HttpHeaders header = new HttpHeaders(); header.set("Authorization", apiKey); HttpEntity entity = new HttpEntity(header); String lista = new String(); HttpEntity<String> response; response = new RestTemplate().exchange("http://ceatic.ujaen.es:8075/radioapi/v2/ratings", HttpMethod.GET, entity, String.class, lista); String canc = response.getBody(); StringTokenizer st = new StringTokenizer(canc, "[", true); st.nextToken();/*w ww . j a v a 2s. c o m*/ if (!st.hasMoreTokens()) { return null; } st.nextToken(); canc = "[" + st.nextToken(); try { ObjectMapper a = new ObjectMapper(); ItemPuntu[] listilla = a.readValue(canc, ItemPuntu[].class); Vector<Integer> ids = new Vector<>(); Vector<Cancion> punt = new Vector<>(); //como jamendo solo devuelve 10 canciones llamamos las veces necesarias for (int i = 0; i < (listilla.length / 10) + 1; ++i) { ids.clear(); //aunque le mandemos mas ids de la cuenta solo devolvera las 10 primeras canciones y //de esta forma controlamos el desborde for (int j = i * 10; j < listilla.length; ++j) { ids.add(listilla[j].getId()); } Cancion[] listilla1 = jamendo.canciones(ids); for (int k = 0; k < listilla1.length; ++k) { punt.add(listilla1[k]); } } for (int i = 0; i < punt.size(); ++i) { punt.get(i).setRating(listilla[i].getRating()); punt.get(i).setFav(listilla[i].getFav()); } return punt.toArray(new Cancion[punt.size()]); } catch (Exception e) { //return null; throw new IOException("no se han recibido canciones"); } }