Example usage for org.springframework.http HttpHeaders add

List of usage examples for org.springframework.http HttpHeaders add

Introduction

In this page you can find the example usage for org.springframework.http HttpHeaders add.

Prototype

@Override
public void add(String headerName, @Nullable String headerValue) 

Source Link

Document

Add the given, single header value under the given name.

Usage

From source file:org.ambraproject.wombat.controller.ArticleController.java

/**
 * Returns a list of figures and tables of a given article; main usage is the figshare tile on the Metrics
 * tab//from  w  w w. java2s.  c  om
 *
 * @param site current site
 * @param articleId DOI identifying the article
 * @return a list of figures and tables of a given article
 * @throws IOException
 */
@RequestMapping(name = "articleFigsAndTables", value = "/article/assets/figsAndTables")
public ResponseEntity<List> listArticleFiguresAndTables(@SiteParam Site site, RequestedDoiVersion articleId)
        throws IOException {
    List<Map<String, ?>> figureView = articleMetadataFactory.get(site, articleId)
            .validateVisibility("articleFigsAndTables").getFigureView();

    HttpHeaders headers = new HttpHeaders();
    headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
    return new ResponseEntity<>(figureView, headers, HttpStatus.OK);
}

From source file:org.apache.fineract.infrastructure.sms.scheduler.SmsMessageScheduledJobServiceImpl.java

/** 
 * get a new HttpEntity with the provided body
 **//*  w  ww.  ja  v  a  2  s . c o  m*/
private HttpEntity<String> getHttpEntity(String body, String apiAuthUsername, String apiAuthPassword) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    String authorization = apiAuthUsername + ":" + apiAuthPassword;

    byte[] encodedAuthorisation = Base64.encode(authorization.getBytes());
    headers.add("Authorization", "Basic " + new String(encodedAuthorisation));

    return new HttpEntity<String>(body, headers);
}

From source file:org.apache.fineract.restwebservice.PlatformRestClient.java

/**
 * Creates a new {@link HttpEntity} object for the HTTP request
 * //  w  w  w.  j av a 2 s. c o  m
 * @param entityBody
 * @param basicAuthenticationCredentials
 * @param contentMediaType
 * @param acceptedResponseMediaTypes
 * @return {@link HttpEntity} object
 */
public HttpEntity<String> getHttpRequestEntity(final String entityBody, final MediaType contentMediaType,
        final List<MediaType> acceptedResponseMediaTypes) {
    final HttpHeaders entityHeaders = new HttpHeaders();

    entityHeaders.setContentType(contentMediaType);
    entityHeaders.setAccept(acceptedResponseMediaTypes);

    if (StringUtils.isNotEmpty(basicAuthenticationCredentials)) {
        entityHeaders.add("Authorization", "Basic " + this.basicAuthenticationCredentials);
    }

    return new HttpEntity<String>(entityBody, entityHeaders);
}

From source file:org.apache.geode.management.internal.web.http.support.HttpRequester.java

void addHeaderValues(HttpHeaders headers) {
    // update the headers
    headers.add(HttpHeaders.USER_AGENT, USER_AGENT_HTTP_REQUEST_HEADER_VALUE);
    headers.setAccept(acceptableMediaTypes);

    if (this.securityProperties != null) {
        for (String key : securityProperties.stringPropertyNames()) {
            headers.add(key, securityProperties.getProperty(key));
        }/* w  w w .  j  a  v a  2  s .c  om*/
    }
}

From source file:org.apache.servicecomb.demo.jaxrs.client.JaxrsClient.java

private static void testJaxRSDefaultValues(RestTemplate template) {
    String microserviceName = "jaxrs";
    for (String transport : DemoConst.transports) {
        CseContext.getInstance().getConsumerProviderManager().setTransport(microserviceName, transport);
        TestMgr.setMsg(microserviceName, transport);

        String cseUrlPrefix = "cse://" + microserviceName + "/JaxRSDefaultValues/";

        //default values
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        String result = template.postForObject(cseUrlPrefix + "/form", request, String.class);
        TestMgr.check("Hello 20bobo", result);

        headers = new HttpHeaders();
        HttpEntity<String> entity = new HttpEntity<>(null, headers);
        result = template.postForObject(cseUrlPrefix + "/header", entity, String.class);
        TestMgr.check("Hello 20bobo30", result);

        result = template.getForObject(cseUrlPrefix + "/query?d=10", String.class);
        TestMgr.check("Hello 20bobo4010", result);
        boolean failed = false;
        try {/*from  w  w  w .  j  a v  a2s.  co  m*/
            result = template.getForObject(cseUrlPrefix + "/query2", String.class);
        } catch (InvocationException e) {
            failed = true;
            TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST);
        }

        failed = false;
        try {
            result = template.getForObject(cseUrlPrefix + "/query2?d=2&e=2", String.class);
        } catch (InvocationException e) {
            failed = true;
            TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST);
        }
        TestMgr.check(failed, true);

        failed = false;
        try {
            result = template.getForObject(cseUrlPrefix + "/query2?a=&d=2&e=2", String.class);
        } catch (InvocationException e) {
            failed = true;
            TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST);
        }
        TestMgr.check(failed, true);

        result = template.getForObject(cseUrlPrefix + "/query2?d=30&e=2", String.class);
        TestMgr.check("Hello 20bobo40302", result);

        failed = false;
        try {
            result = template.getForObject(cseUrlPrefix + "/query3?a=2&b=2", String.class);
        } catch (InvocationException e) {
            failed = true;
            TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST);
        }
        TestMgr.check(failed, true);

        result = template.getForObject(cseUrlPrefix + "/query3?a=30&b=2", String.class);
        TestMgr.check("Hello 302", result);

        result = template.getForObject(cseUrlPrefix + "/query3?a=30", String.class);
        TestMgr.check("Hello 30null", result);

        //input values
        headers = new HttpHeaders();
        headers.setContentType(org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED);
        Map<String, String> params = new HashMap<>();
        params.put("a", "30");
        params.put("b", "sam");
        HttpEntity<Map<String, String>> requestPara = new HttpEntity<>(params, headers);
        result = template.postForObject(cseUrlPrefix + "/form", requestPara, String.class);
        TestMgr.check("Hello 30sam", result);

        headers = new HttpHeaders();
        headers.add("a", "30");
        headers.add("b", "sam");
        headers.add("c", "40");
        entity = new HttpEntity<>(null, headers);
        result = template.postForObject(cseUrlPrefix + "/header", entity, String.class);
        TestMgr.check("Hello 30sam40", result);

        result = template.getForObject(cseUrlPrefix + "/query?a=3&b=sam&c=5&d=30", String.class);
        TestMgr.check("Hello 3sam530", result);

        result = template.getForObject(cseUrlPrefix + "/query2?a=3&b=4&c=5&d=30&e=2", String.class);
        TestMgr.check("Hello 345302", result);
    }
}

From source file:org.apache.servicecomb.demo.jaxrs.client.JaxrsClient.java

private static void testPost(RestTemplate template, String cseUrlPrefix) {
    Map<String, String> params = new HashMap<>();
    params.put("a", "5");
    params.put("b", "3");
    int result = template.postForObject(cseUrlPrefix + "/compute/add", params, Integer.class);
    TestMgr.check(8, result);/*  ww  w . j ava 2  s. c o m*/

    Person person = new Person();
    person.setName("world");
    Person resultPerson = template.postForObject(cseUrlPrefix + "/compute/sayhello", person, Person.class);
    TestMgr.check("hello world", resultPerson.getName());

    HttpHeaders headers = new HttpHeaders();
    headers.add("prefix", "haha");
    HttpEntity<Person> reqEntity = new HttpEntity<>(person, headers);
    TestMgr.check("haha world",
            template.postForObject(cseUrlPrefix + "/compute/saysomething", reqEntity, String.class));
}

From source file:org.apache.servicecomb.demo.jaxrs.client.JaxrsClient.java

private static void testExchange(RestTemplate template, String cseUrlPrefix) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Accept", MediaType.APPLICATION_JSON);
    Person person = new Person();
    person.setName("world");
    HttpEntity<Person> requestEntity = new HttpEntity<>(person, headers);
    ResponseEntity<Person> resEntity = template.exchange(cseUrlPrefix + "/compute/sayhello", HttpMethod.POST,
            requestEntity, Person.class);
    TestMgr.check("hello world", resEntity.getBody());

    ResponseEntity<String> resEntity2 = template.exchange(cseUrlPrefix + "/compute/addstring?s=abc&s=def",
            HttpMethod.DELETE, null, String.class);
    TestMgr.check("abcdef", resEntity2.getBody());
}

From source file:org.apache.servicecomb.demo.jaxrs.client.JaxrsClient.java

@SuppressWarnings({ "rawtypes" })
private static void testValidatorExchangeFail(RestTemplate template, String cseUrlPrefix) {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Accept", MediaType.APPLICATION_JSON);
    Student student = new Student();
    student.setName("");
    student.setAge(25);/*from w w  w .j a  v  a2s.  c o m*/
    boolean isExcep = false;
    try {
        HttpEntity<Student> requestEntity = new HttpEntity<>(student, headers);
        template.exchange(cseUrlPrefix + "/sayhello", HttpMethod.POST, requestEntity, Student.class);
    } catch (InvocationException e) {
        isExcep = true;
        TestMgr.check(400, e.getStatus().getStatusCode());
        TestMgr.check(Status.BAD_REQUEST, e.getReasonPhrase());
        // Message dependends on locale, so just check the short part.
        Map data = (Map) e.getErrorData();
        TestMgr.check(true, data.get("message").toString().contains("propertyPath=sayHello.student.age"));
    }
    TestMgr.check(true, isExcep);
}

From source file:org.apache.servicecomb.demo.springmvc.client.SpringmvcClient.java

private static void testController(RestTemplate template, String microserviceName) {
    String prefix = "cse://" + microserviceName;

    TestMgr.check(7, template.getForObject(prefix + "/controller/add?a=3&b=4", Integer.class));

    try {/*from www  .j  a  va 2s . c  o  m*/
        template.getForObject(prefix + "/controller/add", Integer.class);
        TestMgr.check("failed", "success");
    } catch (InvocationException e) {
        TestMgr.check(e.getStatusCode(), 400);
    }

    TestMgr.check("hi world [world]",
            template.getForObject(prefix + "/controller/sayhi?name=world", String.class));

    TestMgr.check("hi world1 [world1]",
            template.getForObject(prefix + "/controller/sayhi?name={name}", String.class, "world1"));
    TestMgr.check("hi hi  [hi ]",
            template.getForObject(prefix + "/controller/sayhi?name={name}", String.class, "hi "));

    Map<String, String> params = new HashMap<>();
    params.put("name", "world2");
    TestMgr.check("hi world2 [world2]",
            template.getForObject(prefix + "/controller/sayhi?name={name}", String.class, params));

    TestMgr.check("hello world",
            template.postForObject(prefix + "/controller/sayhello/{name}", null, String.class, "world"));
    TestMgr.check("hello hello ",
            template.postForObject(prefix + "/controller/sayhello/{name}", null, String.class, "hello "));

    HttpHeaders headers = new HttpHeaders();
    headers.add("name", "world");
    @SuppressWarnings("rawtypes")
    HttpEntity entity = new HttpEntity<>(null, headers);
    ResponseEntity<String> response = template.exchange(prefix + "/controller/sayhei", HttpMethod.GET, entity,
            String.class);
    TestMgr.check("hei world", response.getBody());

    Person user = new Person();
    user.setName("world");
    TestMgr.check("ha world", template.postForObject(prefix + "/controller/saysomething?prefix={prefix}", user,
            String.class, "ha"));
}

From source file:org.apache.servicecomb.demo.springmvc.client.SpringmvcClient.java

private static void testSpringMvcDefaultValues(RestTemplate template, String microserviceName) {
    String cseUrlPrefix = "cse://" + microserviceName + "/SpringMvcDefaultValues/";
    //default values
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
    String result = template.postForObject(cseUrlPrefix + "/form", request, String.class);
    TestMgr.check("Hello 20bobo", result);

    headers = new HttpHeaders();
    HttpEntity<String> entity = new HttpEntity<>(null, headers);
    result = template.postForObject(cseUrlPrefix + "/header", entity, String.class);
    TestMgr.check("Hello 20bobo30", result);

    result = template.getForObject(cseUrlPrefix + "/query?d=10", String.class);
    TestMgr.check("Hello 20bobo4010", result);
    boolean failed = false;
    try {/*from   w w  w .j  av a 2 s . c om*/
        result = template.getForObject(cseUrlPrefix + "/query2", String.class);
    } catch (InvocationException e) {
        failed = true;
        TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST);
    }

    failed = false;
    try {
        result = template.getForObject(cseUrlPrefix + "/query2?d=2&e=2", String.class);
    } catch (InvocationException e) {
        failed = true;
        TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST);
    }
    TestMgr.check(failed, true);

    failed = false;
    try {
        result = template.getForObject(cseUrlPrefix + "/query2?a=&d=2&e=2", String.class);
    } catch (InvocationException e) {
        failed = true;
        TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST);
    }
    TestMgr.check(failed, true);

    result = template.getForObject(cseUrlPrefix + "/query2?d=30&e=2", String.class);
    TestMgr.check("Hello 20bobo40302", result);

    failed = false;
    try {
        result = template.getForObject(cseUrlPrefix + "/query3?a=2&b=2", String.class);
    } catch (InvocationException e) {
        failed = true;
        TestMgr.check(e.getStatusCode(), HttpStatus.SC_BAD_REQUEST);
    }
    TestMgr.check(failed, true);

    result = template.getForObject(cseUrlPrefix + "/query3?a=30&b=2", String.class);
    TestMgr.check("Hello 302", result);

    result = template.getForObject(cseUrlPrefix + "/query3?a=30", String.class);
    TestMgr.check("Hello 30null", result);

    //input values
    headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    HttpEntity<Map<String, String>> requestPara = new HttpEntity<>(null, headers);
    result = template.postForObject(cseUrlPrefix + "/form?a=30&b=sam", requestPara, String.class);
    TestMgr.check("Hello 30sam", result);

    headers = new HttpHeaders();
    headers.add("a", "30");
    headers.add("b", "sam");
    headers.add("c", "40");
    entity = new HttpEntity<>(null, headers);
    result = template.postForObject(cseUrlPrefix + "/header", entity, String.class);
    TestMgr.check("Hello 30sam40", result);

    result = template.getForObject(cseUrlPrefix + "/query?a=3&b=sam&c=5&d=30", String.class);
    TestMgr.check("Hello 3sam530", result);

    result = template.getForObject(cseUrlPrefix + "/query2?a=3&b=4&c=5&d=30&e=2", String.class);
    TestMgr.check("Hello 345302", result);
}