Friday, May 25, 2018

Writing REST handler in ibm wcs

Writing REST handler: If you are writing a custom handle in WCS follow below steps:

1.       Make entry of your custome handler in resources-ext.properties at below path
Rest\WebContent\WEB-INF\config\resources-ext.properties

e.g com.mycomp.commerce.rest.handlers.ExMyHandler

Now lets try to get value passed in header for GET request

package com.mycomp.commerce.rest.handlers.ExMyHandler;

import java.util.Set;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;

@Path("store/{storeId}/http-header/")
public class HttpHeaderService extends AbstractClassicResourceHandler {
               
                @GET
    @Produces( { "application/atom+xml", "application/json", "application/xml" })
    @Path("/queryheader")
    public Response findHeader(@PathParam("storeId") String storeId, @QueryParam("responseFormat") String responseFormat, , @Context HttpHeaders httpHeaders) {
                String METHODNAME = " findHeader()";
                LOGGER.entering(CLASSNAME,METHODNAME);
        Response result = null;
        String cacheControl = httpHeaders.getRequestHeader("Cache-Control").get(0);
        System.out.println("Cache-Control: "+cacheControl);
        /** get list of all header parameters from request **/
        Set<String> headerKeys = httpHeaders.getRequestHeaders().keySet();
        for(String header:headerKeys){
            System.out.println(header+":"+httpHeaders.getRequestHeader(header).get(0));
        }

        LOGGER.exiting(CLASSNAME,METHODNAME);
System.out.println("----------"+result);
        return result;
    }


Now use below URL in POSTMAN to get header query results:



Results in console : All parameter passed in header will come in system out

[5/25/18 12:40:56:611 IST] 000001ad SystemOut     O Cache-Control: no-cache
[5/25/18 12:40:56:611 IST] 000001ad SystemOut     O Accept:*/*
[5/25/18 12:40:56:611 IST] 000001ad SystemOut     O Accept-Encoding:gzip, deflate
[5/25/18 12:40:56:612 IST] 000001ad SystemOut     O Cache-Control:no-cache
[5/25/18 12:40:56:612 IST] 000001ad SystemOut     O Connection:keep-alive
[5/25/18 12:40:56:612 IST] 000001ad SystemOut     O Content-Type:application/json
[5/25/18 12:40:56:612 IST] 000001ad SystemOut     O Cookie:JSESSIONID=0000XFHI_0g_W1W40QAO34yPPQ8:-1
[5/25/18 12:40:56:613 IST] 000001ad SystemOut     O emailId:pawan@gmail.com
[5/25/18 12:40:56:613 IST] 000001ad SystemOut     O Host:localhost
[5/25/18 12:40:56:613 IST] 000001ad SystemOut     O Postman-Token:d299a6ae-91a6-4710-8a73-223c1b5d63f0
[5/25/18 12:40:56:614 IST] 000001ad SystemOut     O User-Agent:PostmanRuntime/7.1.1
[5/25/18 12:40:56:614 IST] 000001ad SystemOut     O WCToken:125002%2CrbkigQ6NXpcvA2fu2rhe%2BasrZVIbdxvJavsmYi4OFe%2BglUf9Mbq%2F9lz37NMjdk0PvgsSZf5quQghTNL7TTFAbSa03WFEAqmO4tZ5zlRXlCgdQTYfJwH4jY%2B%2FnuRHTaqRjLghrBE%2ByI8jsBbmU80TZQVslYSsLJsSxDIS8hRhaZp9RUjh16nc2iROD7xdj7Q81bPln7rsgYXt6XuttxhQ%2F1uS1jLHY5dM3b07d9pffKj3kT4K6FmA7W%2FRvxLm%2B%2Foh

[5/25/18 12:40:56:614 IST] 000001ad SystemOut     O WCTrustedToken:125002%2CWb5DERrSx6keRM7Ql1yJjh5pYbYy2URYgvSGu0J3cMk%3D

No comments:

Post a Comment