1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.portlet;
21
22 import javax.faces.context.FacesContext;
23 import javax.portlet.ActionRequest;
24 import javax.portlet.PortletContext;
25 import javax.portlet.PortletRequest;
26 import javax.portlet.PortletURL;
27 import javax.portlet.RenderResponse;
28 import java.io.UnsupportedEncodingException;
29
30
31
32
33
34 public final class PortletUtils {
35
36 private static final boolean PORTLET_API_AVAILABLE = portletApiAvailable();
37
38
39
40
41
42
43 private static final String VIEW_ID = PortletUtils.class.getName() + ".VIEW_ID";
44
45 private static boolean portletApiAvailable() {
46 try {
47 return PortletRequest.class != null;
48 } catch (NoClassDefFoundError e) {
49 return false;
50 }
51 }
52
53 private PortletUtils() {
54
55 }
56
57
58
59
60
61
62
63
64 public static boolean isRenderResponse(FacesContext facesContext) {
65 return PORTLET_API_AVAILABLE && facesContext.getExternalContext().getResponse() instanceof RenderResponse;
66 }
67
68
69
70
71
72
73
74
75
76
77
78 public static boolean isPortletRequest(FacesContext facesContext) {
79 return PORTLET_API_AVAILABLE && facesContext.getExternalContext().getContext() instanceof PortletContext;
80 }
81
82 public static String getViewId(FacesContext facesContext) {
83 PortletRequest request = (PortletRequest) facesContext.getExternalContext().getRequest();
84 return request.getParameter(PortletUtils.VIEW_ID);
85 }
86
87
88
89
90 public static String setViewIdForUrl(FacesContext facesContext, String viewId) {
91 RenderResponse response = (RenderResponse) facesContext.getExternalContext().getResponse();
92 PortletURL url = response.createActionURL();
93 url.setParameter(VIEW_ID, viewId);
94 return url.toString();
95 }
96
97 public static void ensureEncoding(FacesContext facesContext) throws UnsupportedEncodingException {
98 ActionRequest request = (ActionRequest) facesContext.getExternalContext().getRequest();
99 if (request.getCharacterEncoding() == null) {
100 request.setCharacterEncoding("UTF-8");
101 }
102 }
103 }