Showing posts with label creating rest services in websphere commerce. Show all posts
Showing posts with label creating rest services in websphere commerce. Show all posts

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

Generating tokens to use REST service in ibm wcs

Generating tokens to use REST service:
Step 1: To get token, Access below REST service with JSON content mention as below

Content to Send:
{
  "logonId" : "abc@gmail.com",
  "logonPassword" : "password"
}
Step 2: In response to above request you will get WCToken and WCTrustedToken which you need to send to further request to access rest of the APIs.
e.g.
{
  "WCToken": "74022%2C7Vc3U%2B7zdArch8iwDFNsSnBJ0odKFI4wkb19WwiWLIwK1tfO%2B4X4tptjpXYoMlHAxtn1iojIdpFj4TUmFV5NGX%2BoxEB1EobxP87w1hm1Sp6Q8adx8pnUVH0UDljzfs5k2FhDroNrcE0K%2F7EpEjEAlLyzT13bBce4af%2BERKPpnDMxxJvc%2Bt8jBR4JfALSnJy6iJ2MVL3vAdwyOvgijRQia6Rs4MWUgIqkzZbuMczAmWU%3D",
  "userId": "74022",
  "personalizationID": "1494826057535-7",
  "WCTrustedToken": "74022%2C%2BB50W0QUpH426rBLfEmg0CD4GpotVzeIvRetPHtY55M%3D",
  "addressId": "27614181409"

}