Search in sources :

Example 91 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project tutorials by eugenp.

the class UserApi method loginUser.

/**
 * Logs user into the system
 *
 * <p><b>200</b> - successful operation
 * <p><b>400</b> - Invalid username/password supplied
 * @param username The user name for login
 * @param password The password for login in clear text
 * @return String
 * @throws RestClientException if an error occurs while attempting to invoke the API
 */
public String loginUser(String username, String password) throws RestClientException {
    Object postBody = null;
    // verify the required parameter 'username' is set
    if (username == null) {
        throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'username' when calling loginUser");
    }
    // verify the required parameter 'password' is set
    if (password == null) {
        throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Missing the required parameter 'password' when calling loginUser");
    }
    String path = UriComponentsBuilder.fromPath("/user/login").build().toUriString();
    final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
    final HttpHeaders headerParams = new HttpHeaders();
    final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();
    queryParams.putAll(apiClient.parameterToMultiValueMap(null, "username", username));
    queryParams.putAll(apiClient.parameterToMultiValueMap(null, "password", password));
    final String[] accepts = { "application/xml", "application/json" };
    final List<MediaType> accept = apiClient.selectHeaderAccept(accepts);
    final String[] contentTypes = {};
    final MediaType contentType = apiClient.selectHeaderContentType(contentTypes);
    String[] authNames = new String[] {};
    ParameterizedTypeReference<String> returnType = new ParameterizedTypeReference<String>() {
    };
    return apiClient.invokeAPI(path, HttpMethod.GET, queryParams, postBody, headerParams, formParams, accept, contentType, authNames, returnType);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MediaType(org.springframework.http.MediaType)

Example 92 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project java-chassis by ServiceComb.

the class SpringMvcIntegrationTestBase method blowsUpWhenFileNameDoesNotMatch.

@Test
public void blowsUpWhenFileNameDoesNotMatch() throws Exception {
    String file1Content = "hello world";
    String file2Content = "bonjour";
    MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    map.add("file1", new FileSystemResource(newFile(file1Content).getAbsolutePath()));
    map.add("unmatched name", new FileSystemResource(newFile(file2Content).getAbsolutePath()));
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    ResponseEntity<String> response = null;
    try {
        response = restTemplate.postForEntity(codeFirstUrl + "uploadWithoutAnnotation", new HttpEntity<>(map, headers), String.class);
        assertEquals("required is true, throw exception", "but not throw exception");
    } catch (HttpClientErrorException e) {
        assertEquals(400, e.getRawStatusCode());
        assertEquals("Bad Request", e.getStatusCode().getReasonPhrase());
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) FileSystemResource(org.springframework.core.io.FileSystemResource) Test(org.junit.Test)

Example 93 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project java-chassis by ServiceComb.

the class MultiErrorCodeServiceClient method testErrorCodeWrongType.

private static void testErrorCodeWrongType() {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    String body = "{\"message\":\"hello\",\"code\":\"wrongType\"";
    HttpEntity<String> entity = new HttpEntity<>(body, headers);
    ResponseEntity<MultiResponse200> result;
    try {
        template.postForEntity(serverDirectURL + "/MultiErrorCodeService/errorCode", entity, MultiResponse200.class);
    } catch (HttpClientErrorException e) {
        TestMgr.check(e.getRawStatusCode(), 400);
        TestMgr.check(e.getMessage(), "400 Bad Request: \"{\"message\":\"Parameter is not valid for operation [jaxrs.MultiErrorCodeService.errorCode]." + " Parameter is [request]. Processor is [body].\"}\"");
    }
    entity = new HttpEntity<>(null, headers);
    try {
        result = template.postForEntity(serverDirectURL + "/MultiErrorCodeService/errorCode", entity, MultiResponse200.class);
        TestMgr.check(590, 200);
    } catch (HttpServerErrorException e) {
        TestMgr.check(e.getRawStatusCode(), 500);
    }
    // not recommend
    body = "{\"message\":\"hello\",\"code\":\"200\"}";
    entity = new HttpEntity<>(body, headers);
    result = template.postForEntity(serverDirectURL + "/MultiErrorCodeService/errorCode", entity, MultiResponse200.class);
    TestMgr.check(result.getStatusCodeValue(), 200);
    TestMgr.check(result.getBody().getMessage(), "success result");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) MultiResponse200(org.apache.servicecomb.demo.multiErrorCode.MultiResponse200) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException)

Example 94 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project goci by EBISPOT.

the class DepositionPublicationServiceImpl method retrieveSubmission.

@Override
public DepositionSubmission retrieveSubmission(String id) {
    log.info("Retrieving submission using id [{}]", id);
    DepositionSubmission submission = null;
    Map<String, String> params = new HashMap<>();
    params.put("submissionID", id);
    String url = depositionIngestUri + "/submissions/{submissionID}";
    try {
        String response = template.getForObject(url, String.class, params);
        submission = template.getForObject(url, DepositionSubmission.class, params);
    } catch (HttpClientErrorException e) {
        System.out.println(e.getMessage());
    }
    return submission;
}
Also used : DepositionSubmission(uk.ac.ebi.spot.goci.model.deposition.DepositionSubmission) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HashMap(java.util.HashMap)

Example 95 with HttpClientErrorException

use of org.springframework.web.client.HttpClientErrorException in project goci by EBISPOT.

the class DepositionPublicationServiceImpl method getAllBackendPublications.

@Override
public Map<String, DepositionPublication> getAllBackendPublications() {
    log.info("Retrieving publications");
    String url = depositionBackendUri + "/publications?page={page}&size=100";
    Map<String, DepositionPublication> publicationMap = new HashMap<>();
    try {
        int i = 0;
        Map<String, Integer> params = new HashMap<>();
        params.put("page", i);
        String response = template.getForObject(url, String.class, params);
        DepositionPublicationListWrapper publications = template.getForObject(url, DepositionPublicationListWrapper.class, params);
        while (i < publications.getPage().getTotalPages()) {
            addPublications(publicationMap, publications);
            params.put("page", ++i);
            publications = template.getForObject(url, DepositionPublicationListWrapper.class, params);
        }
    } catch (HttpClientErrorException e) {
        System.out.println(e.getMessage());
    }
    return publicationMap;
}
Also used : HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) DepositionPublication(uk.ac.ebi.spot.goci.model.deposition.DepositionPublication) HashMap(java.util.HashMap) DepositionPublicationListWrapper(uk.ac.ebi.spot.goci.model.deposition.util.DepositionPublicationListWrapper)

Aggregations

HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)109 HttpHeaders (org.springframework.http.HttpHeaders)31 Test (org.junit.Test)25 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)24 HashMap (java.util.HashMap)21 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)21 MediaType (org.springframework.http.MediaType)19 RestTemplate (org.springframework.web.client.RestTemplate)19 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)18 HttpEntity (org.springframework.http.HttpEntity)16 URI (java.net.URI)15 Map (java.util.Map)8 HttpStatus (org.springframework.http.HttpStatus)8 RestClientException (org.springframework.web.client.RestClientException)8 ResourceAccessException (org.springframework.web.client.ResourceAccessException)7 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)6 Date (java.util.Date)5 List (java.util.List)5 OpenAppNamespaceDTO (com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO)4 ArrayList (java.util.ArrayList)4