Search in sources :

Example 81 with HttpEntity

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());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap)

Example 82 with HttpEntity

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()));
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpEntity(org.springframework.http.HttpEntity) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XmlObject(com.cloud.utils.xmlobject.XmlObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 83 with HttpEntity

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());
}
Also used : HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Link(org.apache.geode.management.internal.web.domain.Link) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 84 with HttpEntity

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);
}
Also used : ClickatellRequestEntity(org.hisp.dhis.sms.outbound.ClickatellRequestEntity) HttpEntity(org.springframework.http.HttpEntity) HttpStatus(org.springframework.http.HttpStatus)

Example 85 with HttpEntity

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);
    }
}
Also used : SystemInfo(org.hisp.dhis.system.SystemInfo) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) HttpEntity(org.springframework.http.HttpEntity) HttpStatus(org.springframework.http.HttpStatus) HttpHeadersBuilder(org.hisp.dhis.system.util.HttpHeadersBuilder) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) ResourceAccessException(org.springframework.web.client.ResourceAccessException)

Aggregations

HttpEntity (org.springframework.http.HttpEntity)85 HttpHeaders (org.springframework.http.HttpHeaders)62 Test (org.junit.Test)46 URI (java.net.URI)15 ResponseEntity (org.springframework.http.ResponseEntity)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 ArrayList (java.util.ArrayList)11 MediaType (org.springframework.http.MediaType)11 RestTemplate (org.springframework.web.client.RestTemplate)11 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 MultiValueMap (org.springframework.util.MultiValueMap)9 List (java.util.List)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)7 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)7 HttpStatus (org.springframework.http.HttpStatus)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)6 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5