use of org.springframework.http.HttpEntity in project java-chassis by ServiceComb.
the class CodeFirstRestTemplate method testCodeFirstReduce.
protected void testCodeFirstReduce(RestTemplate template, String cseUrlPrefix) {
Map<String, String> params = new HashMap<>();
params.put("a", "5");
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.COOKIE, "b=3");
HttpEntity<?> requestEntity = new HttpEntity<>(headers);
ResponseEntity<Integer> result = template.exchange(cseUrlPrefix + "reduce?a={a}", HttpMethod.GET, requestEntity, Integer.class, params);
TestMgr.check(2, result.getBody());
}
use of org.springframework.http.HttpEntity in project cloudstack by apache.
the class Force10BaremetalSwitchBackend method prepareVlan.
@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 {
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()));
}
}
use of org.springframework.http.HttpEntity in project geode by apache.
the class ClientHttpRequestJUnitTest method testCreateRequestEntityForPost.
@Test
@SuppressWarnings("unchecked")
public void testCreateRequestEntityForPost() throws Exception {
final Link expectedLink = new Link("post", toUri("http://host.domain.com:8080/app/libraries/{name}/books"), HttpMethod.POST);
final ClientHttpRequest request = new ClientHttpRequest(expectedLink);
assertEquals(expectedLink, request.getLink());
final MultiValueMap<String, Object> expectedRequestParameters = new LinkedMultiValueMap<String, Object>(4);
expectedRequestParameters.add("author", "Douglas Adams");
expectedRequestParameters.add("title", "The Hitchhiker's Guide to the Galaxy");
expectedRequestParameters.add("year", "1979");
expectedRequestParameters.add("isbn", "0345453743");
request.addHeaderValues(HttpHeader.CONTENT_TYPE.getName(), MediaType.APPLICATION_FORM_URLENCODED_VALUE);
request.addParameterValues("author", expectedRequestParameters.getFirst("author"));
request.addParameterValues("title", expectedRequestParameters.getFirst("title"));
request.addParameterValues("year", expectedRequestParameters.getFirst("year"));
request.addParameterValues("isbn", expectedRequestParameters.getFirst("isbn"));
final HttpEntity<MultiValueMap<String, Object>> requestEntity = (HttpEntity<MultiValueMap<String, Object>>) request.createRequestEntity();
assertNotNull(requestEntity);
assertNotNull(requestEntity.getHeaders());
assertEquals(MediaType.APPLICATION_FORM_URLENCODED, requestEntity.getHeaders().getContentType());
assertEquals(expectedRequestParameters, requestEntity.getBody());
}
use of org.springframework.http.HttpEntity in project dhis2-core by dhis2.
the class ClickatellGateway method send.
@Override
public OutboundMessageResponse send(String subject, String text, Set<String> recipients, SmsGatewayConfig config) {
ClickatellGatewayConfig clickatellConfiguration = (ClickatellGatewayConfig) config;
HttpEntity<ClickatellRequestEntity> request = new HttpEntity<>(getRequestBody(text, recipients), getRequestHeaderParameters(clickatellConfiguration));
HttpStatus httpStatus = send(clickatellConfiguration.getUrlTemplate() + MAX_MESSAGE_PART, request, ClickatellResponseEntity.class);
return wrapHttpStatus(httpStatus);
}
use of org.springframework.http.HttpEntity in project dhis2-core by dhis2.
the class DefaultMonitoringService method pushMonitoringInfo.
@Override
public void pushMonitoringInfo() {
String url = (String) systemSettingManager.getSystemSetting(SettingKey.SYSTEM_MONITORING_URL);
String username = (String) systemSettingManager.getSystemSetting(SettingKey.SYSTEM_MONITORING_USERNAME);
String password = (String) systemSettingManager.getSystemSetting(SettingKey.SYSTEM_MONITORING_PASSWORD);
if (StringUtils.isBlank(url)) {
log.debug("Monitoring service URL not configured, aborting monitoring request");
return;
}
SystemInfo systemInfo = systemService.getSystemInfo();
if (systemInfo == null) {
log.warn("System info not available, aborting monitoring request");
return;
}
systemInfo.clearSensitiveInfo();
HttpHeadersBuilder headersBuilder = new HttpHeadersBuilder().withContentTypeJson();
if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
headersBuilder.withBasicAuth(username, password);
}
HttpEntity<SystemInfo> requestEntity = new HttpEntity<>(systemInfo, headersBuilder.build());
ResponseEntity<String> response = null;
HttpStatus sc = null;
try {
response = restTemplate.postForEntity(url, requestEntity, String.class);
sc = response.getStatusCode();
} catch (HttpClientErrorException | HttpServerErrorException ex) {
log.warn("Monitoring request failed, status code: " + sc, ex);
return;
} catch (ResourceAccessException ex) {
log.info("Monitoring request failed, network is unreachable");
return;
}
if (response != null && sc != null && sc.is2xxSuccessful()) {
log.debug("Monitoring request successfully sent");
} else {
log.warn("Monitoring request was unsuccessful, status code: " + sc);
}
}
Aggregations