论坛

主页 » Liferay Portal » English » 3. Development

组合视图 统一视图 树状图
讨论主题 [ 上一个 | 下一个 ]
toggle
miii suuu
How is Portlet get user-agent from httpHeader ?
2008年7月15日 上午11:35
答复

miii suuu

等级: New Member

帖子: 24

加入日期: 2008年7月5日

最近的帖子

Hi all,

I tried to get user-agent from httpHeader but still not found any way to do that. I am using portlet JSR 168

Thanks,
suu
Wilson Man
RE: How is Portlet get user-agent from httpHeader ?
2008年7月15日 下午5:54
答复

Wilson Man

LIFERAY STAFF

等级: Liferay Master

帖子: 537

加入日期: 2006年6月21日

最近的帖子

hmmm, i don't think the httpHeader info is found in portletRequest. You may have to gain access to the HttpServletRequest or you can extend Liferay to put that info into the portletRequest attribute.
miii suuu
RE: How is Portlet get user-agent from httpHeader ?
2008年7月16日 上午9:25
答复

miii suuu

等级: New Member

帖子: 24

加入日期: 2008年7月5日

最近的帖子

Many thank for your reply.
I have concern about method getProperty(String name ) of javax.portlet.PortletRequest. Could you please explain for me about that, as I know it used to get attributes from httpHeader, eg : portletRequest.getProperty("user-agent");

If you could, please explain me a simple example description that PorletRequest can httpHeader.

Thank you so much

suu

PS: I have just approached Portlet some week ago emoticon
Wilson Man
RE: How is Portlet get user-agent from httpHeader ?
2008年7月16日 上午10:00
答复

Wilson Man

LIFERAY STAFF

等级: Liferay Master

帖子: 537

加入日期: 2006年6月21日

最近的帖子

actually, i check on the API and you may want to try the getProperties() method and see if you can get the header values returned.
miii suuu
RE: How is Portlet get user-agent from httpHeader ?
2008年7月16日 上午10:24
答复

miii suuu

等级: New Member

帖子: 24

加入日期: 2008年7月5日

最近的帖子

Yup, I tried with both of them - getProperty() and getProperties() but could not get any info about httpHeader.
I used portlet in strust webwork, below a example I tried.

 1
 2PortletRequest portletRequest = com.opensymphony.webwork.portlet.context.PortletActionContext.getRequest();
 3       
 4        final Enumeration e = portletRequest.getPropertyNames();
 5        String value = null;
 6        String name = null;
 7        while (e.hasMoreElements()) {
 8            name = (String) e.nextElement();
 9            value = portletRequest.getProperty(name);
10           System.out.println(value);
11        }


Also, I tried with simple method getProperty("user-agent") but still can not.

- suuu
Wilson Man
RE: How is Portlet get user-agent from httpHeader ?
2008年7月16日 上午10:48
答复

Wilson Man

LIFERAY STAFF

等级: Liferay Master

帖子: 537

加入日期: 2006年6月21日

最近的帖子

can you post the system out printing the name value pairs?
miii suuu
RE: How is Portlet get user-agent from httpHeader ?
2008年7月16日 上午11:04
答复

miii suuu

等级: New Member

帖子: 24

加入日期: 2008年7月5日

最近的帖子

Hi,

have not any data in Enumeration - e.hasMoreElements() always = null

not reach to while loop, there for do not any is printed out.

-suuu
Fuad Efendi
RE: How is Portlet get user-agent from httpHeader ?
2008年7月16日 上午11:51
答复

Fuad Efendi

等级: Regular Member

帖子: 148

加入日期: 2007年4月5日

最近的帖子

Try this:

((com.liferay.portlet.PortletRequestImpl) portletRequest).getHttpServletRequest().getHeader("user-agent");
or something similar.

JSR-168 mentions only some HTTP headers available to PortletRequest (such as Locale, Content-Length).



miii suuu:
Hi all,

I tried to get user-agent from httpHeader but still not found any way to do that. I am using portlet JSR 168

Thanks,
suu
miii suuu
RE: How is Portlet get user-agent from httpHeader ?
2008年7月16日 下午12:29
答复

miii suuu

等级: New Member

帖子: 24

加入日期: 2008年7月5日

最近的帖子

Many thanks for your instruction about JSR168 spec.
1
2PortletRequest portletRequest = com.opensymphony.webwork.portlet.context.PortletActionContext.getRequest();
3String portletRequestContext = "";
4portletRequestContext.append(...)
5....
6portletRequestContext.append(portletRequest.getContextPath());
7System.out.println(portletRequestContext);


Will print out : http://localhost:8080/portletexample, that mean portletRequest have effect.
And then:

1
2portletRequest.getProperty("content-length"); // will return ""
3portletRequest.getProperty("content-type");// will return ""
4portletRequest.getProperty("accept-language"); //will return ""

I only concern that, in this case I do as JSR168 spec, I would like to know why I can not get httpHeader ?

Also I use portletRequest.getPropertyNames() method, but I get null value too,

- suuu
miii suuu
RE: How is Portlet get user-agent from httpHeader ?
2008年7月16日 下午12:44
答复

miii suuu

等级: New Member

帖子: 24

加入日期: 2008年7月5日

最近的帖子

Hi,

I debugged and saw that object requestPortlet is com.liferay.portlet.RenderRequestImp

suu
Fuad Efendi
RE: How is Portlet get user-agent from httpHeader ?
2008年7月16日 下午1:40
答复

Fuad Efendi

等级: Regular Member

帖子: 148

加入日期: 2007年4月5日

最近的帖子

miii suuu:
Many thanks for your instruction about JSR168 spec.
1
2portletRequest.getProperty("content-length"); // will return ""
3portletRequest.getProperty("content-type");// will return ""
4portletRequest.getProperty("accept-language"); //will return ""

I only concern that, in this case I do as JSR168 spec, I would like to know why I can not get httpHeader ?

Also I use portletRequest.getPropertyNames() method, but I get null value too,

- suuu



JSR-168:
=======

getLocale()
Returns the preferred Locale in which the portal will accept content.

getResponseContentType()
Returns the portal preferred content type for the response.


HTTP Headers available to PortletRequest depend also on whether it is ActionRequest:

getCharacterEncoding()
Returns the name of the character encoding used in the body of this request.
getContentLength()
Returns the length, in bytes, of the request body which is made available by the input stream, or -1 if the length is not known.
getContentType()
Returns the MIME type of the body of the request, or null if the type is not known.
... ... ...


Only some HTTP headers available explicitly (for file upload request for instance), and not via getProperty()... getProperty() could return HTTP headers, but it's up to Portlet Container implementation... "A portlet can access portal/portlet-container specific properties through this method and, if available, the headers of the HTTP client request."


((com.liferay.portlet.PortletRequestImpl) portletRequest).getHttpServletRequest().getHeader("user-agent");
works fine; I tested it with RenderRequest instance.



com.liferay.portlet.PortletRequestImpl

...
public Enumeration<String> getPropertyNames() {
return _portalCtx.getPropertyNames();
}
...
public HttpServletRequest getHttpServletRequest() {
return _req;
}
...
gianluca maranzana
RE: How is Portlet get user-agent from httpHeader ?
2008年8月19日 上午9:33
答复

gianluca maranzana

等级: Junior Member

帖子: 26

加入日期: 2008年7月30日

最近的帖子

...And what about getHeader("x-forwarded-for")?

I'm try to get a client's IP inside a portlet, but my liferay is proxed via ajp with an Apache, and getRemoteAddr/Host doesn't work.

Any Ideas?

Tnx
gianluca maranzana
RE: How is Portlet get user-agent from httpHeader ?
2008年8月20日 上午6:59
答复

gianluca maranzana

等级: Junior Member

帖子: 26

加入日期: 2008年7月30日

最近的帖子

I reply by myself:

I've noticed that in my portlet Jsp, the "request" Object is a "com.liferay.portlet.PortletServletRequest".

In 4.4.2 (that I'm using...) this class have "some" methods such as getRemoteAddr()/Host() that forced return in null.

If you cast your "request" to the class that extends it (javax.servlet.http.HttpServletRequestWrapper) you get quickly the correct IP from getRemoteAddr()/Host(), also if you're using reverseProxy w/ Apache.

G.
daniel aguil mallea
RE: How is Portlet get user-agent from httpHeader ?
2009年4月8日 上午11:38
答复

daniel aguil mallea

等级: New Member

帖子: 16

加入日期: 2007年2月23日

最近的帖子

other tip...

<jsp:scriptlet>
boolean isFirefox = request.getHeader("user-agent").indexOf("Firefox/") != -1 ;
if(isFirefox){
</jsp:scriptlet>
....
...
....