View Javadoc

1   /*
2    * $Id: PortletUrlRenderer.java 651946 2008-04-27 13:41:38Z apetrelli $
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.util.PortletUrlHelper;
29  
30  import com.opensymphony.xwork2.ActionContext;
31  import com.opensymphony.xwork2.ActionInvocation;
32  import com.opensymphony.xwork2.util.TextUtils;
33  
34  /***
35   * Implementation of the {@link UrlRenderer} interface that renders URLs for portlet environments.
36   * 
37   * @see UrlRenderer
38   *
39   */
40  public class PortletUrlRenderer implements UrlRenderer {
41  	
42  	/***
43  	 * {@inheritDoc}
44  	 */
45  	public void renderUrl(Writer writer, URL urlComponent) {
46  		String scheme = urlComponent.req.getScheme();
47  
48  		if (urlComponent.scheme != null) {
49  			scheme = urlComponent.scheme;
50  		}
51  
52          String result;
53          urlComponent.namespace = urlComponent.determineNamespace(urlComponent.namespace, urlComponent.stack, urlComponent.req);
54          if (onlyActionSpecified(urlComponent)) {
55                  result = PortletUrlHelper.buildUrl(urlComponent.action, urlComponent.namespace, urlComponent.method, urlComponent.parameters, urlComponent.portletUrlType, urlComponent.portletMode, urlComponent.windowState);
56          } else if(onlyValueSpecified(urlComponent)){
57                  result = PortletUrlHelper.buildResourceUrl(urlComponent.value, urlComponent.parameters);
58          }
59          else {
60          	result = createDefaultUrl(urlComponent);
61          }
62          if ( urlComponent.anchor != null && urlComponent.anchor.length() > 0 ) {
63              result += '#' + urlComponent.anchor;
64          }
65  
66          String var = urlComponent.getVar();
67  
68          if (var != null) {
69              urlComponent.putInContext(result);
70  
71              // add to the request and page scopes as well
72              urlComponent.req.setAttribute(var, result);
73          } else {
74              try {
75                  writer.write(result);
76              } catch (IOException e) {
77                  throw new StrutsException("IOError: " + e.getMessage(), e);
78              }
79          }
80  	}
81  
82  	private String createDefaultUrl(URL urlComponent) {
83  		String result;
84  		ActionInvocation ai = (ActionInvocation)urlComponent.getStack().getContext().get(
85  				ActionContext.ACTION_INVOCATION);
86  		String action = ai.getProxy().getActionName();
87  		result = PortletUrlHelper.buildUrl(action, urlComponent.namespace, urlComponent.method, urlComponent.parameters, urlComponent.portletUrlType, urlComponent.portletMode, urlComponent.windowState);
88  		return result;
89  	}
90  
91  	private boolean onlyValueSpecified(URL urlComponent) {
92  		return urlComponent.value != null && urlComponent.action == null;
93  	}
94  
95  	private boolean onlyActionSpecified(URL urlComponent) {
96  		return urlComponent.value == null && urlComponent.action != null;
97  	}
98  
99  	/***
100 	 * {@inheritDoc}
101 	 */
102 	public void renderFormUrl(Form formComponent) {
103 		String namespace = formComponent.determineNamespace(formComponent.namespace, formComponent.getStack(),
104 				formComponent.request);
105 		String action = null;
106         if (formComponent.action != null) {
107             action = formComponent.findString(formComponent.action);
108         }
109         else {
110         	ActionInvocation ai = (ActionInvocation) formComponent.getStack().getContext().get(ActionContext.ACTION_INVOCATION);
111         	action = ai.getProxy().getActionName();
112         }
113         String type = "action";
114         if (TextUtils.stringSet(formComponent.method)) {
115             if ("GET".equalsIgnoreCase(formComponent.method.trim())) {
116                 type = "render";
117             }
118         }
119         if (action != null) {
120             String result = PortletUrlHelper.buildUrl(action, namespace, null,
121                     formComponent.getParameters(), type, formComponent.portletMode, formComponent.windowState);
122             formComponent.addParameter("action", result);
123 
124 
125             // name/id: cut out anything between / and . should be the id and
126             // name
127             String id = formComponent.getId();
128             if (id == null) {
129                 int slash = action.lastIndexOf('/');
130                 int dot = action.indexOf('.', slash);
131                 if (dot != -1) {
132                     id = action.substring(slash + 1, dot);
133                 } else {
134                     id = action.substring(slash + 1);
135                 }
136                 formComponent.addParameter("id", formComponent.escape(id));
137             }
138         }
139 
140 		
141 	}
142 
143 	public void beforeRenderUrl(URL urlComponent) {
144 	}
145 
146 }