use of org.springframework.web.client.RestTemplate in project spring-boot-admin by codecentric.
the class ApplicationRegistratorTest method setup.
@Before
public void setup() {
restTemplate = mock(RestTemplate.class);
adminProps = new AdminProperties();
adminProps.setUrl(new String[] { "http://sba:8080", "http://sba2:8080" });
ApplicationFactory factory = mock(ApplicationFactory.class);
when(factory.createApplication()).thenReturn(Application.create("AppName").withManagementUrl("http://localhost:8080/mgmt").withHealthUrl("http://localhost:8080/health").withServiceUrl("http://localhost:8080").build());
registrator = new ApplicationRegistrator(restTemplate, adminProps, factory);
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
}
use of org.springframework.web.client.RestTemplate in project ETSMobile-Android2 by ApplETS.
the class MonETSNotificationsRequest method loadDataFromNetwork.
@Override
public MonETSNotificationList loadDataFromNetwork() throws Exception {
accountManager = AccountManager.get(context);
Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
if (accounts.length > 0) {
authToken = accountManager.peekAuthToken(accounts[0], Constants.AUTH_TOKEN_TYPE);
}
String url = context.getString(R.string.portail_api_base_url);
if (onlyNewNotifs) {
url += "/api/notification/dossier/1";
} else {
url += "/api/notification";
}
ClientHttpRequestInterceptor interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
requestWrapper.getHeaders().set("Cookie", authToken);
return execution.execute(requestWrapper, body);
}
};
RestTemplate restTemplate = getRestTemplate();
List<ClientHttpRequestInterceptor> list = new ArrayList<ClientHttpRequestInterceptor>();
list.add(interceptor);
restTemplate.setInterceptors(list);
try {
return restTemplate.getForObject(url, MonETSNotificationList.class);
} catch (HttpClientErrorException e) {
if (e.getStatusCode().value() == 401) {
if (accounts.length > 0) {
accountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
authToken = accountManager.blockingGetAuthToken(accounts[0], Constants.AUTH_TOKEN_TYPE, true);
interceptor = new ClientHttpRequestInterceptor() {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
requestWrapper.getHeaders().set("Cookie", authToken);
return execution.execute(requestWrapper, body);
}
};
list.clear();
list.add(interceptor);
restTemplate.setInterceptors(list);
}
}
} finally {
return restTemplate.getForObject(url, MonETSNotificationList.class);
}
}
use of org.springframework.web.client.RestTemplate in project OpenClinica by OpenClinica.
the class XformMetaDataService method getMediaFileItemFromFormManager.
public FileItem getMediaFileItemFromFormManager(String fileLink, String crfOid, String formLayoutOid) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM));
HttpEntity<String> entity = new HttpEntity<String>(headers);
ResponseEntity<byte[]> response = restTemplate.exchange(fileLink, HttpMethod.GET, entity, byte[].class, "1");
FileItem fileItem = null;
if (response.getStatusCode() == HttpStatus.OK) {
String fileName = "";
FileOutputStream output = null;
try {
String disposition = response.getHeaders().get("Content-Disposition").get(0);
fileName = disposition.replaceFirst("(?i)^.*filename=\"([^\"]+)\".*$", "$1");
String dir = Utils.getCrfMediaFilePath(crfOid, formLayoutOid);
if (!new File(dir).exists()) {
new File(dir).mkdirs();
logger.debug("Made the directory " + dir);
}
File file = new File(dir + fileName);
output = new FileOutputStream(file);
IOUtils.write(response.getBody(), output);
fileItem = new DiskFileItem("media_file", response.getHeaders().get("Content-Type").get(0), false, fileName, 100000000, file);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (output != null) {
output.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return fileItem;
}
use of org.springframework.web.client.RestTemplate in project OpenClinica by OpenClinica.
the class XformMetaDataService method executeIndividualCrf.
public void executeIndividualCrf(ExecuteIndividualCrfObject eicObject) {
for (OCodmComplexTypeDefinitionFormLayoutDef formLayoutDef : eicObject.formLayoutDefs) {
List<String> fileLinks = null;
String vForm = "";
RestTemplate rest = new RestTemplate();
if (eicObject.form != null) {
List<FormVersion> versions = eicObject.form.getVersions();
for (FormVersion version : versions) {
if (version.getName().equals(formLayoutDef.getOID())) {
fileLinks = version.getFileLinks();
for (String fileLink : fileLinks) {
if (fileLink.endsWith(VERSION)) {
vForm = rest.getForObject(fileLink, String.class);
break;
}
}
if (!eicObject.errors.hasErrors()) {
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<XformGroup>> mapType = new TypeReference<List<XformGroup>>() {
};
List<XformGroup> jsonList = null;
try {
jsonList = mapper.readValue(vForm, mapType);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XformContainer xformContainer = new XformContainer();
xformContainer.setGroups(jsonList);
eicObject.setContainer(xformContainer);
if (eicObject.errors.hasErrors()) {
return;
}
// Save meta-data in database
saveFormMetadata(eicObject, version, eicObject.container, formLayoutDef, fileLinks);
}
}
}
}
}
}
use of org.springframework.web.client.RestTemplate in project OpenClinica by OpenClinica.
the class EnketoAPI method getURL.
private EnketoURLResponse getURL(URL url, String crfOID) {
try {
String userPasswdCombo = new String(Base64.encodeBase64((token + ":").getBytes()));
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "Basic " + userPasswdCombo);
headers.add("Accept-Charset", "UTF-8");
EnketoURLRequest body = new EnketoURLRequest(ocURL, crfOID);
HttpEntity<EnketoURLRequest> request = new HttpEntity<EnketoURLRequest>(body, headers);
RestTemplate rest = new RestTemplate();
ResponseEntity<EnketoURLResponse> response = rest.postForEntity(url.toString(), request, EnketoURLResponse.class);
if (response != null)
return response.getBody();
else
return null;
} catch (Exception e) {
logger.error(e.getMessage());
logger.error(ExceptionUtils.getStackTrace(e));
}
return null;
}
Aggregations