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);
}
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());
}
}
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");
}
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;
}
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;
}
Aggregations