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:com.nautestech.VERDE.controller.HomeController.java

/**
 * Simply selects the home view to render by returning its name.
 *///ww  w . j ava 2 s.c o  m
//   @RequestMapping(value = "/", method = RequestMethod.GET)
//   public String home(Locale locale, Model model) {
//      logger.info("Welcome home! The client locale is {}.", locale);
//      
//      Date date = new Date();
//      DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
//      
//      String formattedDate = dateFormat.format(date);
//      
//      model.addAttribute("serverTime", formattedDate );
//      
//      return "home1";
//   }

@RequestMapping(value = "/verdeLicense")
public String RestVerdeLicense(Model model) throws URISyntaxException {

    RestTemplate restTemplate = new RestTemplate();

    String plainCreds = "mcadmin1:pass_1234";
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + base64Creds);
    //      headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    HttpEntity<String> request = new HttpEntity<String>(headers);
    URI url = new URI("http://netseason.iptime.org/mc/rest/verdeLicense");
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class);

    logger.info(response.getHeaders().toString());

    model.addAttribute("result", response.getBody());

    return "rest";
}

From source file:com.nautestech.VERDE.controller.HomeController.java

@RequestMapping(value = "/verdeUser")
public String Rest(Model model) throws URISyntaxException {

    RestTemplate restTemplate = new RestTemplate();

    String plainCreds = "mcadmin1:pass_1234";
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + base64Creds);
    //      headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

    HttpEntity<String> request = new HttpEntity<String>(headers);
    URI url = new URI("http://netseason.iptime.org/mc/rest/verdeUser");
    ResponseEntity<VerdeUser> response = restTemplate.exchange(url, HttpMethod.GET, request, VerdeUser.class);

    logger.info(response.getHeaders().toString());

    List<XmlData> list = new ArrayList<XmlData>();
    list = response.getBody().getVerdeUser();
    String result = "";
    for (int i = 0; i < list.size(); i++) {
        XmlData data = (XmlData) list.get(i);
        result += data.getName() + "|" + data.getValue() + "</br>\n";
    }//from  w w w .  ja v  a2 s.c o m

    logger.info(result);

    model.addAttribute("result", result);

    return "rest";
}

From source file:com.orange.ngsi.client.NgsiClient.java

/**
 * The default HTTP request headers, depends on the host supporting xml or json
 * @param url//from w  w  w .  java 2s. c  om
 * @return the HTTP request headers.
 */
public HttpHeaders getRequestHeaders(String url, HttpServletRequest request) {
    HttpHeaders requestHeaders = new HttpHeaders();

    MediaType mediaType = MediaType.APPLICATION_JSON;
    //        if (request != null && request.getAttribute("Content-Type") != null 
    //              && !request.getAttribute("Content-Type").equals("application/json")){
    ////           MediaType mediaType = MediaType.APPLICATION_JSON;
    //           if (url == null || protocolRegistry.supportXml(url)) {
    //               mediaType = MediaType.APPLICATION_XML;
    //           }
    //        }
    if ((url == null || protocolRegistry.supportXml(url)) && !isJsonContentType(request)) {
        mediaType = MediaType.APPLICATION_XML;
    }

    requestHeaders.setContentType(mediaType);
    requestHeaders.setAccept(Collections.singletonList(mediaType));

    if (request != null) {
        requestHeaders.add("Fiware-Service", request.getHeader("Fiware-Service"));
        requestHeaders.add("Fiware-ServicePath", request.getHeader("Fiware-ServicePath"));
    }

    return requestHeaders;
}

From source file:com.rsa.redchallenge.standaloneapp.utils.RestInteractor.java

/**
 * Function to call GET interface for a REST server
 *
 * @param response Format in this the response of the call needs to be parsed
 * @param path     path of the interface in the REST server to be called
 * @param params   parameter for the call
 * @param <T>      template class according to which response needs to be parsed
 * @return returns the response in format specified
 * @throws RestClientException throws in case of error
 *//*w  ww.  j  a  v  a  2 s  .c  o m*/
public static <T> T performGet(Class<T> response, String path, Map<String, Object> params, String jsessionId)
        throws SecurityException {
    RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
    setFactory(restTemplate);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Cookie", "RSA_SA_LICENSE=true; " + jsessionId);
    String uri = populatePath(params, path);
    logger.info("performing get request for uri:" + uri);
    HttpEntity<String> request = new HttpEntity<>(headers);
    try {
        T result = restTemplate.exchange(uri, HttpMethod.GET, request, response, getObjectParams(params))
                .getBody();
        return result;
    } catch (HttpClientErrorException e) {
        throw e;
    }
}

From source file:com.rsa.redchallenge.standaloneapp.utils.RestInteractor.java

/**
 * Function to call POST interface for a REST server
 *
 * @param path path of the interface in the REST server to be called
 * @return returns the response/*  w w  w  . j a va 2s  . c  o  m*/
 */
public static String performPost(String path, Map<String, Object> params, String jsessionId)
        throws SecurityException {

    RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
    setFactory(restTemplate);
    HttpHeaders headers = new HttpHeaders();
    headers.add("Cookie", "RSA_SA_LICENSE=true; " + jsessionId);
    String uri = populatePath(params, path);
    logger.info("performing post request for uri:" + uri);
    HttpEntity<String> request = new HttpEntity<>(headers);

    try {
        String result = restTemplate.exchange(uri, HttpMethod.GET, request, String.class).getBody();
        return result;
    } catch (HttpClientErrorException e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:com.sitewhere.groovy.device.communication.rest.RestHelper.java

/**
 * Perform a GET request to the given relative URL.
 * //from w w  w  .  j  a  v a2s  .  c o  m
 * @param relativeUrl
 * @param responseType
 * @return
 * @throws SiteWhereSystemException
 */
protected <T> T get(String relativeUrl, Class<T> responseType) throws SiteWhereException {
    try {
        HttpHeaders headers = new HttpHeaders();
        if (!StringUtils.isEmpty(getUsername()) && !StringUtils.isEmpty(getPassword())) {
            headers.add("Authorization", getAuthHeader());
        }
        HttpEntity<Void> entity = new HttpEntity<Void>(headers);
        String url = baseUrl + relativeUrl;
        ResponseEntity<T> response = client.exchange(url, HttpMethod.GET, entity, responseType);
        return response.getBody();
    } catch (ResourceAccessException e) {
        throw new SiteWhereException(e);
    }
}

From source file:com.sitewhere.rest.client.SiteWhereClient.java

/**
 * Send a REST request and handle the response.
 * //from   w  w w .  j  av  a 2s .co m
 * @param url
 * @param method
 * @param input
 * @param clazz
 * @param vars
 * @return
 * @throws SiteWhereSystemException
 */
protected <S, T> S sendRest(String url, HttpMethod method, T input, Class<S> clazz, Map<String, String> vars)
        throws SiteWhereSystemException {
    try {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", getAuthHeader());
        headers.add(ISiteWhereWebConstants.HEADER_TENANT_TOKEN, getTenantAuthToken());
        HttpEntity<T> entity = new HttpEntity<T>(input, headers);
        ResponseEntity<S> response = getClient().exchange(url, method, entity, clazz, vars);
        return response.getBody();
    } catch (ResourceAccessException e) {
        if (e.getCause() instanceof SiteWhereSystemException) {
            throw (SiteWhereSystemException) e.getCause();
        }
        throw new RuntimeException(e);
    }
}

From source file:com.sitewhere.rest.client.SiteWhereClient.java

protected <S, T> S sendBinary(String url, HttpMethod method, T input, Class<S> clazz, Map<String, String> vars)
        throws SiteWhereSystemException {
    try {//from   w  w w  .  j  a va  2  s. c o m
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", getAuthHeader());
        headers.add(ISiteWhereWebConstants.HEADER_TENANT_TOKEN, getTenantAuthToken());
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        HttpEntity<T> entity = new HttpEntity<T>(input, headers);
        ResponseEntity<S> response = getClient().exchange(url, method, entity, clazz, vars);
        return response.getBody();
    } catch (ResourceAccessException e) {
        if (e.getCause() instanceof SiteWhereSystemException) {
            throw (SiteWhereSystemException) e.getCause();
        }
        throw new RuntimeException(e);
    }
}

From source file:com.sitewhere.rest.client.SiteWhereClient.java

/**
 * Send a REST request and return response as byte array.
 * /*from  w w  w .j a v a2s .  co m*/
 * @param url
 * @param method
 * @param vars
 * @return
 * @throws SiteWhereSystemException
 */
protected <T> byte[] sendRestWithBinaryResponse(String url, HttpMethod method, Map<String, String> vars)
        throws SiteWhereSystemException {
    try {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", getAuthHeader());
        headers.add(ISiteWhereWebConstants.HEADER_TENANT_TOKEN, getTenantAuthToken());

        ResponseEntity<byte[]> response = getClient().exchange(url, method, new HttpEntity<byte[]>(headers),
                byte[].class, vars);

        return response.getBody();
    } catch (ResourceAccessException e) {
        if (e.getCause() instanceof SiteWhereSystemException) {
            throw (SiteWhereSystemException) e.getCause();
        }
        throw new RuntimeException(e);
    }
}

From source file:com.sitewhere.wso2.identity.scim.Wso2ScimAssetModule.java

/**
 * Perform a REST get operation and return a marshaled result.
 * /*from  w w w.  j  a  v a2  s .  c  o  m*/
 * @param url
 * @param clazz
 * @return
 * @throws SiteWhereException
 */
protected <S> S doGet(String url, Class<S> clazz) throws SiteWhereException {
    try {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", getAuthHeader());
        HttpEntity<Void> entity = new HttpEntity<Void>(headers);
        ResponseEntity<S> response = client.exchange(url, HttpMethod.GET, entity, clazz);
        return response.getBody();
    } catch (ResourceAccessException e) {
        throw new SiteWhereException("REST call failed.", e);
    }
}