/
BridgeNavigationHandlerImpl.java
179 lines (138 loc) · 6.29 KB
/
BridgeNavigationHandlerImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Copyright (c) 2000-2022 Liferay, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.liferay.faces.bridge.application.internal;
import java.util.Iterator;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.application.NavigationCase;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.portlet.PortletConfig;
import javax.portlet.PortletContext;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.portlet.StateAwareResponse;
import javax.portlet.faces.Bridge;
import com.liferay.faces.bridge.BridgeFactoryFinder;
import com.liferay.faces.bridge.BridgeURL;
import com.liferay.faces.bridge.BridgeURLFactory;
import com.liferay.faces.bridge.scope.internal.BridgeRequestScope;
import com.liferay.faces.bridge.util.internal.RequestMapUtil;
import com.liferay.faces.bridge.util.internal.ViewUtil;
import com.liferay.faces.util.logging.Logger;
import com.liferay.faces.util.logging.LoggerFactory;
/**
* @author Neil Griffin
*/
public class BridgeNavigationHandlerImpl extends BridgeNavigationHandlerCompatImpl {
// Logger
private static final Logger logger = LoggerFactory.getLogger(BridgeNavigationHandlerImpl.class);
public BridgeNavigationHandlerImpl(NavigationHandler navigationHandler) {
super(navigationHandler);
}
@Override
public void handleNavigation(FacesContext facesContext, String fromAction, String outcome) {
logger.debug("fromAction=[{0}] outcome=[{1}]", fromAction, outcome);
String queryString = null;
UIViewRoot uiViewRoot = facesContext.getViewRoot();
String viewId = uiViewRoot.getViewId();
if (viewId != null) {
int pos = viewId.indexOf("?");
if (pos > 0) {
queryString = viewId.substring(pos);
viewId = viewId.substring(0, pos);
uiViewRoot.setViewId(viewId);
}
}
NavigationCase navigationCase = getNavigationCase(facesContext, fromAction, outcome);
// Ask the wrapped NavigationHandler to perform the navigation.
getWrappedNavigationHandler().handleNavigation(facesContext, fromAction, outcome);
if (queryString != null) {
uiViewRoot.setViewId(viewId.concat(queryString));
}
if (navigationCase != null) {
// Hack for http://jira.icesoft.org/browse/ICE-7996
Iterator<FacesMessage> itr = facesContext.getMessages();
while (itr.hasNext()) {
FacesMessage facesMessage = itr.next();
if (facesMessage.getDetail().contains("Unable to find matching navigation case")) {
logger.warn("Removed bogus FacesMessage caused by http://jira.icesoft.org/browse/ICE-7996");
itr.remove();
}
}
// If the navigation-case is NOT a redirect, then directly encode the {@link PortletMode} and {@link
// WindowState} to the response. Don't need to worry about the redirect case here because that's handled in
// the ExternalContext#redirect(String) method. It would be nice to handle the redirect case here but it
// needs to stay in ExternalContext since it's possible for developers to call
// ExternalContext.redirect(String) directly from their application.
if (!navigationCase.isRedirect()) {
String toViewId = navigationCase.getToViewId(facesContext);
if (toViewId != null) {
ExternalContext externalContext = facesContext.getExternalContext();
PortletResponse portletResponse = (PortletResponse) externalContext.getResponse();
if (portletResponse instanceof StateAwareResponse) {
PortletContext portletContext = (PortletContext) externalContext.getContext();
BridgeURLFactory bridgeURLFactory = (BridgeURLFactory) BridgeFactoryFinder.getFactory(
portletContext, BridgeURLFactory.class);
try {
BridgeURL bridgeActionURL = bridgeURLFactory.getBridgeActionURL(facesContext, toViewId);
BridgeNavigationCase bridgeNavigationCase = new BridgeNavigationCaseImpl(navigationCase);
String portletMode = bridgeNavigationCase.getPortletMode();
if (portletMode != null) {
bridgeActionURL.setParameter(Bridge.PORTLET_MODE_PARAMETER, portletMode);
}
String windowState = bridgeNavigationCase.getWindowState();
if (windowState != null) {
bridgeActionURL.setParameter(Bridge.PORTLET_WINDOWSTATE_PARAMETER, windowState);
}
PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
BridgeNavigationUtil.navigate(portletRequest, (StateAwareResponse) portletResponse,
bridgeActionURL.getParameterMap());
}
catch (Exception e) {
logger.error(e.getMessage());
}
}
}
}
}
}
@Override
public void handleNavigation(FacesContext facesContext, PortletMode fromPortletMode, PortletMode toPortletMode) {
if ((fromPortletMode != null) && !fromPortletMode.equals(toPortletMode)) {
logger.debug("fromPortletMode=[{0}] toPortletMode=[{1}]", fromPortletMode, toPortletMode);
String currentViewId = facesContext.getViewRoot().getViewId();
PortletConfig portletConfig = RequestMapUtil.getPortletConfig(facesContext);
Map<String, String> defaultViewIdMap = ViewUtil.getDefaultViewIdMap(portletConfig);
String portletModeViewId = defaultViewIdMap.get(toPortletMode.toString());
if ((currentViewId != null) && (portletModeViewId != null)) {
if (!currentViewId.equals(portletModeViewId)) {
logger.debug("Navigating to viewId=[{0}]", portletModeViewId);
ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
UIViewRoot viewRoot = viewHandler.createView(facesContext, portletModeViewId);
if (viewRoot != null) {
facesContext.setViewRoot(viewRoot);
partialViewContextRenderAll(facesContext);
}
}
}
}
}
}