Clint Barbosa
Clint Barbosa

Reputation: 71

How to get HttpServletRequest in Liferay WebService

Hi I have a webService that is generated from buildServices of Liferay.. the method looks like this

 public User getUserTest(long userId) {

    User u = null;
    try {
        Token token = OAuthFactoryUtil.createToken("sasa", "sdad");
    } catch (OAuthException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        u = UserLocalServiceUtil.getUser(userId);
        System.out.println("xx user " + u.getScreenName());
    } catch (Exception e) {
        System.out.println(" Exception ************* " + e.toString());
    }

    return u;
} 

the parameters in this ws would be this :

http://localhost:8080/demo-portlet/api/json?serviceClassName=com.sample.portlet.library.service.BookServiceUtil&serviceMethodName=getUserTest&userId=10195&serviceParameters=[userId]

having userId as a parameter..

How would you pass a parameter if you need HttpServletRequest.. my method would look like this

public User getUserTest(HttpServletRequest httpRequest) {

    User u = null;

    try {

        String version = httpRequest.getHeader("X-PHM-APP-VERSION");
        Token token = OAuthFactoryUtil.createToken("sasa", "sdad");
    } catch (OAuthException e1) {
        e1.printStackTrace();
    }

    try {
        String authorization = httpRequest.getHeader("Authorization");
        u = UserLocalServiceUtil.getUser(Long.valueOf(authorization));
        System.out.println("authorization --> " + authorization);
        System.out.println("xx user " + u.getScreenName());
    } catch (Exception e) {
        System.out.println(" Exception ************* " + e.toString());
    }

    return u;
}

I need the HttpServletRequest to get the parameters from header, instead of passing through url. Is there a better way to get parameters from header? thanks for your help

Upvotes: 2

Views: 8531

Answers (2)

Sandeep Nair
Sandeep Nair

Reputation: 3650

I think webservice layer is normally at a later stage where in you would never pass request. Ideally what you would do is pass header parameter to the webservice instead of request

Upvotes: 2

Felix Christy
Felix Christy

Reputation: 2193

In Liferay, you will get HttpServletRequest from the PortletRequest. Please use com.liferay.portal.util.PortalUtil class.

There are 2 methods in it. getHttpServletRequest() and getOriginalServletRequest(), you will get the both core level http request from these methods.

Upvotes: 0

Related Questions