Search in sources :

Example 41 with HttpEntity

use of org.springframework.http.HttpEntity in project metron by apache.

the class RestTestingUtil method getPcapsByIdentifiers.

private static void getPcapsByIdentifiers(String srcIp, String dstIp, String protocol, String srcPort, String dstPort) {
    System.out.println("**********************getPcapsByKeysRange ******************************************************************************************");
    // 1.
    String url = "http://" + hostName + "/pcapGetter/getPcapsByIdentifiers?srcIp={srcIp}" + "&dstIp={dstIp}" + "&protocol={protocol}" + "&srcPort={srcPort}" + "&dstPort={dstPort}";
    // default values
    String startTime = "-1";
    String endTime = "-1";
    String maxResponseSize = "6";
    @SuppressWarnings("rawtypes") Map map = new HashMap();
    map.put("srcIp", srcIp);
    map.put("dstIp", dstIp);
    map.put("protocol", protocol);
    map.put("srcPort", srcPort);
    map.put("dstPort", dstPort);
    RestTemplate template = new RestTemplate();
    // set headers and entity to send
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_OCTET_STREAM_VALUE);
    HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
    // 1.
    ResponseEntity<byte[]> response1 = template.exchange(url, HttpMethod.GET, requestEntity, byte[].class, map);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.format("getPcapsByIdentifiers : request= <srcIp=%s; dstIp=%s; protocol=%s; endTime=%s; srcPort=%s; dstPort=%s> \n response= %s \n", srcIp, dstIp, protocol, endTime, srcPort, dstPort, response1);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.println();
    // 2. with time range
    startTime = System.getProperty("startTime", "-1");
    endTime = System.getProperty("endTime", "-1");
    map.put("startTime", startTime);
    map.put("endTime", endTime);
    ResponseEntity<byte[]> response2 = template.exchange(url, HttpMethod.GET, requestEntity, byte[].class, map);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.format("getPcapsByIdentifiers : request= <srcIp=%s; dstIp=%s; protocol=%s; endTime=%s; srcPort=%s; dstPort=%s> \n response= %s \n", srcIp, dstIp, protocol, endTime, srcPort, dstPort, response2);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.println();
    // 3. with maxResponseSize
    maxResponseSize = System.getProperty("maxResponseSize", "6");
    map.put("maxResponseSize", maxResponseSize);
    ResponseEntity<byte[]> response3 = template.exchange(url, HttpMethod.GET, requestEntity, byte[].class, map);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.format("getPcapsByIdentifiers : request= <srcIp=%s; dstIp=%s; protocol=%s; endTime=%s; srcPort=%s; dstPort=%s> \n response= %s \n", srcIp, dstIp, protocol, endTime, srcPort, dstPort, response3);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.println();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) RestTemplate(org.springframework.web.client.RestTemplate) Map(java.util.Map) HashMap(java.util.HashMap)

Example 42 with HttpEntity

use of org.springframework.http.HttpEntity in project metron by apache.

the class RestTestingUtil method getPcapsByKeysRange.

/**
 * Gets the pcaps by keys range.
 *
 * @param startKey
 *          the start key
 * @param endKey
 *          the end key
 * @return the pcaps by keys range
 */
@SuppressWarnings("unchecked")
private static void getPcapsByKeysRange(String startKey, String endKey) {
    System.out.println("**********************getPcapsByKeysRange ******************************************************************************************");
    // 1.
    String url = "http://" + hostName + "/cisco-rest/pcapGetter/getPcapsByKeyRange?startKey={startKey}" + "&endKey={endKey}" + "&startTime={startTime}" + "&endTime={endTime}" + "&maxResponseSize={maxResponseSize}";
    // default values
    String startTime = "-1";
    String endTime = "-1";
    String maxResponseSize = "6";
    @SuppressWarnings("rawtypes") Map map = new HashMap();
    map.put("startKey", startKey);
    map.put("endKey", "endKey");
    map.put("startTime", startTime);
    map.put("endTime", endTime);
    map.put("maxResponseSize", maxResponseSize);
    RestTemplate template = new RestTemplate();
    // set headers and entity to send
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_OCTET_STREAM_VALUE);
    HttpEntity<Object> requestEntity = new HttpEntity<Object>(headers);
    // 1.
    ResponseEntity<byte[]> response1 = template.exchange(url, HttpMethod.GET, requestEntity, byte[].class, map);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.format("getPcapsByKeysRange : request= <startKey=%s; endKey=%s; startTime=%s; endTime=%s; maxResponseSize=%s> \n response= %s \n", startKey, endKey, startTime, endTime, maxResponseSize, response1);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.println();
    // 2. with time range
    startTime = System.getProperty("startTime", "-1");
    endTime = System.getProperty("endTime", "-1");
    map.put("startTime", startTime);
    map.put("endTime", endTime);
    ResponseEntity<byte[]> response2 = template.exchange(url, HttpMethod.GET, requestEntity, byte[].class, map);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.format("getPcapsByKeysRange : request= <startKey=%s; endKey=%s; startTime=%s; endTime=%s; maxResponseSize=%s> \n response= %s \n", startKey, endKey, startTime, endTime, maxResponseSize, response2);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.println();
    // 3. with maxResponseSize
    maxResponseSize = System.getProperty("maxResponseSize", "6");
    map.put("maxResponseSize", maxResponseSize);
    ResponseEntity<byte[]> response3 = template.exchange(url, HttpMethod.GET, requestEntity, byte[].class, map);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.format("getPcapsByKeysRange : request= <startKey=%s; endKey=%s; startTime=%s; endTime=%s; maxResponseSize=%s> \n response= %s \n", startKey, endKey, startTime, endTime, maxResponseSize, response3);
    System.out.println("----------------------------------------------------------------------------------------------------");
    System.out.println();
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) RestTemplate(org.springframework.web.client.RestTemplate) Map(java.util.Map) HashMap(java.util.HashMap)

Example 43 with HttpEntity

use of org.springframework.http.HttpEntity in project cas by apereo.

the class RestPasswordManagementService method findEmail.

@Override
public String findEmail(final String username) {
    final PasswordManagementProperties.Rest rest = properties.getRest();
    if (StringUtils.isBlank(rest.getEndpointUrlEmail())) {
        return null;
    }
    final HttpHeaders headers = new HttpHeaders();
    headers.setAccept(CollectionUtils.wrap(MediaType.APPLICATION_JSON));
    headers.put("username", CollectionUtils.wrap(username));
    final HttpEntity<String> entity = new HttpEntity<>(headers);
    final ResponseEntity<String> result = restTemplate.exchange(rest.getEndpointUrlEmail(), HttpMethod.GET, entity, String.class);
    if (result.getStatusCodeValue() == HttpStatus.OK.value() && result.hasBody()) {
        return result.getBody();
    }
    return null;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) PasswordManagementProperties(org.apereo.cas.configuration.model.support.pm.PasswordManagementProperties)

Example 44 with HttpEntity

use of org.springframework.http.HttpEntity in project cas by apereo.

the class ClickatellSmsSender method send.

@Override
public boolean send(final String from, final String to, final String message) {
    try {
        final MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("Authorization", this.token);
        headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
        final Map<String, Object> map = new HashMap<>();
        map.put("content", message);
        map.put("to", CollectionUtils.wrap(to));
        map.put("from", from);
        final StringWriter stringify = new StringWriter();
        mapper.writeValue(stringify, map);
        final HttpEntity<String> request = new HttpEntity<>(stringify.toString(), headers);
        final ResponseEntity<Map> response = restTemplate.postForEntity(new URI(this.serverUrl), request, Map.class);
        if (response.hasBody()) {
            final List<Map> messages = (List<Map>) response.getBody().get("messages");
            final String error = (String) response.getBody().get("error");
            if (StringUtils.isNotBlank(error)) {
                LOGGER.error(error);
                return false;
            }
            final List<String> errors = messages.stream().filter(m -> m.containsKey("accepted") && !Boolean.parseBoolean(m.get("accepted").toString()) && m.containsKey("error")).map(m -> (String) m.get("error")).collect(Collectors.toList());
            if (errors.isEmpty()) {
                return true;
            }
            errors.forEach(LOGGER::error);
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return false;
}
Also used : MediaType(org.springframework.http.MediaType) StringWriter(java.io.StringWriter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MultiValueMap(org.springframework.util.MultiValueMap) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) HttpEntity(org.springframework.http.HttpEntity) List(java.util.List) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) Map(java.util.Map) CollectionUtils(org.apereo.cas.util.CollectionUtils) ResponseEntity(org.springframework.http.ResponseEntity) URI(java.net.URI) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) RestTemplate(org.springframework.web.client.RestTemplate) SmsSender(org.apereo.cas.util.io.SmsSender) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HashMap(java.util.HashMap) URI(java.net.URI) StringWriter(java.io.StringWriter) List(java.util.List) MultiValueMap(org.springframework.util.MultiValueMap) HashMap(java.util.HashMap) Map(java.util.Map) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 45 with HttpEntity

use of org.springframework.http.HttpEntity in project cas by apereo.

the class RestConsentRepository method findConsentDecisions.

@Override
public Collection<ConsentDecision> findConsentDecisions() {
    try {
        final HttpHeaders headers = new HttpHeaders();
        headers.setAccept(CollectionUtils.wrap(MediaType.APPLICATION_JSON));
        final HttpEntity<String> entity = new HttpEntity<>(headers);
        final ResponseEntity<List> result = restTemplate.exchange(this.endpoint, HttpMethod.GET, entity, List.class);
        if (result.getStatusCodeValue() == HttpStatus.OK.value()) {
            return result.getBody();
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return new ArrayList<>(0);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ToString(lombok.ToString)

Aggregations

HttpEntity (org.springframework.http.HttpEntity)104 HttpHeaders (org.springframework.http.HttpHeaders)81 Test (org.junit.Test)46 RestTemplate (org.springframework.web.client.RestTemplate)17 URI (java.net.URI)15 ArrayList (java.util.ArrayList)13 ResponseEntity (org.springframework.http.ResponseEntity)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 Map (java.util.Map)11 MediaType (org.springframework.http.MediaType)11 HashMap (java.util.HashMap)10 List (java.util.List)10 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)10 MultiValueMap (org.springframework.util.MultiValueMap)9 ByteArrayInputStream (java.io.ByteArrayInputStream)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