1 16 package org.apache.cocoon.components.modules.input; 17 18 import org.apache.cocoon.environment.ObjectModelHelper; 19 import org.apache.cocoon.environment.Request; 20 import org.apache.cocoon.environment.portlet.PortletEnvironment; 21 import org.apache.cocoon.environment.portlet.PortletObjectModelHelper; 22 import org.apache.cocoon.util.NetUtils; 23 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.thread.ThreadSafe; 27 28 import javax.portlet.PortletURL; 29 import javax.portlet.RenderResponse; 30 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Map ; 36 37 54 public class PortletURLModule extends AbstractInputModule implements ThreadSafe { 55 56 public static final String NAME_RENDER = "render"; 57 public static final String NAME_RESOURCE = "resource"; 58 public static final String NAME_ACTION = "action"; 59 60 private static final String PREFIX_RENDER = NAME_RENDER + ":"; 61 private static final String PREFIX_RESOURCE = NAME_RESOURCE + ":"; 62 private static final String PREFIX_ACTION = NAME_ACTION + ":"; 63 64 private static final List returnNames; 65 66 static { 67 List tmp = new ArrayList (); 68 tmp.add(NAME_RENDER); 69 tmp.add(NAME_RESOURCE); 70 tmp.add(NAME_ACTION); 71 returnNames = tmp; 72 } 73 74 public Iterator getAttributeNames(Configuration modeConf, Map objectModel) throws ConfigurationException { 75 return PortletURLModule.returnNames.iterator(); 76 } 77 78 public Object getAttribute(String name, Configuration modeConf, Map objectModel) throws ConfigurationException { 79 if (name == null) { 80 throw new NullPointerException ("Attribute name is null"); 81 } 82 83 Request request = ObjectModelHelper.getRequest(objectModel); 84 85 RenderResponse renderResponse = PortletObjectModelHelper.getRenderResponse(objectModel); 86 if (renderResponse != null) { 87 PortletURL url = null; 88 if (name.startsWith(PREFIX_RENDER)) { 89 url = renderResponse.createRenderURL(); 90 name = name.substring(PREFIX_RENDER.length()); 91 if (name.length() > 0 && name.charAt(0) == '/') { 92 name = name.substring(1); 93 } 94 } else if (name.startsWith(PREFIX_RESOURCE)) { 95 name = name.substring(PREFIX_RESOURCE.length()); 96 if (name.length() == 0 || name.charAt(0) != '/') { 97 String uri = request.getContextPath() + "/" + request.getServletPath(); 98 name = NetUtils.absolutize(uri, name); 99 } 100 return renderResponse.encodeURL(name); 101 } else if (name.startsWith(PREFIX_ACTION)) { 102 url = renderResponse.createActionURL(); 103 name = name.substring(PREFIX_ACTION.length()); 104 if (name.length() > 0 && name.charAt(0) == '/') { 105 name = name.substring(1); 106 } 107 } else { 108 throw new IllegalArgumentException ("Invalid attribute name '" + name + "' for '" + getClass().getName() + "'"); 109 } 110 111 Map parameters = new HashMap (7); 112 name = NetUtils.deparameterize(name, parameters); 113 if (name.length() > 0) { 114 parameters.put(PortletEnvironment.PARAMETER_PATH_INFO, name); 115 } 116 for (Iterator i = parameters.entrySet().iterator(); i.hasNext();) { 117 Map.Entry me = (Map.Entry )i.next(); 118 String param = (String )me.getKey(); 119 Object values = me.getValue(); 120 if (values instanceof String ) { 121 url.setParameter(param, (String ) values); 122 } else { 123 url.setParameter(param, (String []) values); 124 } 125 } 126 return url.toString(); 127 } 128 if (name.startsWith(PREFIX_RENDER)) { 129 return name.substring(PREFIX_RENDER.length()); 130 } else if (name.startsWith(PREFIX_RESOURCE)) { 131 return name.substring(PREFIX_RESOURCE.length()); 132 } else if (name.startsWith(PREFIX_ACTION)) { 133 return name.substring(PREFIX_ACTION.length()); 134 } else { 135 throw new IllegalArgumentException ("Invalid attribute name '" + name + "' for '" + getClass().getName() + "'"); 136 } 137 } 138 139 public Object [] getAttributeValues(String name, Configuration modeConf, Map objectModel) throws ConfigurationException { 140 Object result = getAttribute(name, modeConf, objectModel); 141 if (result != null) { 142 return new Object []{result}; 143 } 144 return null; 145 } 146 } 147 | Popular Tags |