View Javadoc

1   /*
2    * $Id: PortletUrlRenderer.java 724182 2008-12-07 19:32:45Z nilsga $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.components;
23  
24  import java.io.IOException;
25  import java.io.Writer;
26  
27  import org.apache.struts2.StrutsException;
28  import org.apache.struts2.portlet.context.PortletActionContext;
29  import org.apache.struts2.portlet.util.PortletUrlHelper;
30  
31  import com.opensymphony.xwork2.ActionContext;
32  import com.opensymphony.xwork2.ActionInvocation;
33  import com.opensymphony.xwork2.util.TextUtils;
34  
35  /***
36   * Implementation of the {@link UrlRenderer} interface that renders URLs for portlet environments.
37   * 
38   * @see UrlRenderer
39   *
40   */
41  public class PortletUrlRenderer implements UrlRenderer {
42  	
43  	/***
44  	 * The servlet renderer used when not executing in a portlet context.
45  	 */
46  	private UrlRenderer servletRenderer = null;
47  	
48  	public PortletUrlRenderer() {
49  		this.servletRenderer = new ServletUrlRenderer();
50  	}
51  	
52  	/***
53  	 * {@inheritDoc}
54  	 */
55  	public void renderUrl(Writer writer, URL urlComponent) {
56  		if(PortletActionContext.getPortletContext() == null || "none".equalsIgnoreCase(urlComponent.portletUrlType)) {
57  			servletRenderer.renderUrl(writer, urlComponent);
58  		}
59  		else {
60  			String action = null;
61  			if(urlComponent.action != null) {
62  				action = urlComponent.findString(urlComponent.action);
63  			}
64  			String scheme = urlComponent.req.getScheme();
65  
66  			if (urlComponent.scheme != null) {
67  				scheme = urlComponent.scheme;
68  			}
69  
70  			String result;
71  			urlComponent.namespace = urlComponent.determineNamespace(urlComponent.namespace, urlComponent.stack, urlComponent.req);
72  			if (onlyActionSpecified(urlComponent)) {
73  				result = PortletUrlHelper.buildUrl(action, urlComponent.namespace, urlComponent.method, urlComponent.parameters, urlComponent.portletUrlType, urlComponent.portletMode, urlComponent.windowState);
74  			} else if(onlyValueSpecified(urlComponent)){
75  				result = PortletUrlHelper.buildResourceUrl(urlComponent.value, urlComponent.parameters);
76  			}
77  			else {
78  				result = createDefaultUrl(urlComponent);
79  			}
80  			if ( urlComponent.anchor != null && urlComponent.anchor.length() > 0 ) {
81  				result += '#' + urlComponent.findString(urlComponent.anchor);
82  			}
83  
84  			String var = urlComponent.getVar();
85  
86  			if (var != null) {
87  				urlComponent.putInContext(result);
88  
89  				// add to the request and page scopes as well
90  				urlComponent.req.setAttribute(var, result);
91  			} else {
92  				try {
93  					writer.write(result);
94  				} catch (IOException e) {
95  					throw new StrutsException("IOError: " + e.getMessage(), e);
96  				}
97  			}
98  		}
99  	}
100 
101 	private String createDefaultUrl(URL urlComponent) {
102 		String result;
103 		ActionInvocation ai = (ActionInvocation)urlComponent.getStack().getContext().get(
104 				ActionContext.ACTION_INVOCATION);
105 		String action = ai.getProxy().getActionName();
106 		result = PortletUrlHelper.buildUrl(action, urlComponent.namespace, urlComponent.method, urlComponent.parameters, urlComponent.portletUrlType, urlComponent.portletMode, urlComponent.windowState);
107 		return result;
108 	}
109 
110 	private boolean onlyValueSpecified(URL urlComponent) {
111 		return urlComponent.value != null && urlComponent.action == null;
112 	}
113 
114 	private boolean onlyActionSpecified(URL urlComponent) {
115 		return urlComponent.value == null && urlComponent.action != null;
116 	}
117 
118 	/***
119 	 * {@inheritDoc}
120 	 */
121 	public void renderFormUrl(Form formComponent) {
122 		if(PortletActionContext.getPortletContext() == null) {
123 			servletRenderer.renderFormUrl(formComponent);
124 		}
125 		else {
126 			String namespace = formComponent.determineNamespace(formComponent.namespace, formComponent.getStack(),
127 					formComponent.request);
128 			String action = null;
129 			if (formComponent.action != null) {
130 				action = formComponent.findString(formComponent.action);
131 			}
132 			else {
133 				ActionInvocation ai = (ActionInvocation) formComponent.getStack().getContext().get(ActionContext.ACTION_INVOCATION);
134 				action = ai.getProxy().getActionName();
135 			}
136 			String type = "action";
137 			if (TextUtils.stringSet(formComponent.method)) {
138 				if ("GET".equalsIgnoreCase(formComponent.method.trim())) {
139 					type = "render";
140 				}
141 			}
142 			if (action != null) {
143 				String result = PortletUrlHelper.buildUrl(action, namespace, null,
144 						formComponent.getParameters(), type, formComponent.portletMode, formComponent.windowState);
145 				formComponent.addParameter("action", result);
146 
147 
148 				// name/id: cut out anything between / and . should be the id and
149 				// name
150 				String id = formComponent.getId();
151 				if (id == null) {
152 					int slash = action.lastIndexOf('/');
153 					int dot = action.indexOf('.', slash);
154 					if (dot != -1) {
155 						id = action.substring(slash + 1, dot);
156 					} else {
157 						id = action.substring(slash + 1);
158 					}
159 					formComponent.addParameter("id", formComponent.escape(id));
160 				}
161 			}
162 		}
163 		
164 	}
165 
166 	public void beforeRenderUrl(URL urlComponent) {
167 		if(PortletActionContext.getPortletContext() == null) {
168 			servletRenderer.beforeRenderUrl(urlComponent);
169 		}
170 	}
171 
172 	public void setServletRenderer(UrlRenderer nonPortletRenderer) {
173 		this.servletRenderer = nonPortletRenderer;
174 		
175 	}
176 
177 }