1 9 package org.jboss.portal.server.impl.invocation; 10 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Map ; 14 15 import javax.servlet.http.HttpServletRequest ; 16 import javax.servlet.http.HttpServletResponse ; 17 18 import org.apache.log4j.Logger; 19 import org.jboss.portal.common.value.ConversionException; 20 import org.jboss.portal.server.PortalServer; 21 import org.jboss.portal.server.ServerObject; 22 import org.jboss.portal.server.ServerObjectID; 23 import org.jboss.portal.server.ServerURL; 24 import org.jboss.portal.server.invocation.AttachmentKey; 25 import org.jboss.portal.server.invocation.Interceptor; 26 import org.jboss.portal.server.invocation.Invocation; 27 import org.jboss.portal.server.util.Parameters; 28 import org.jboss.portal.server.util.ServerObjectIDConverter; 29 30 36 public class InvocationImpl 37 implements Invocation 38 { 39 40 41 protected static Logger log = Logger.getLogger(InvocationImpl.class); 42 43 44 private static final Interceptor[] EMPTY_INTERCEPTORS = new Interceptor[0]; 45 46 47 private Interceptor[] interceptors; 48 49 50 private int currentInterceptor; 51 52 53 protected final Map attachments; 54 55 56 public Parameters controlParameters; 57 58 59 public Parameters targetParameters; 60 61 62 public ServerObject target; 63 64 65 public PortalServer container; 66 67 68 public int nonSecurePort; 69 70 71 public int securePort; 72 73 74 public PortalRequestImpl req; 75 76 77 public PortalResponseImpl resp; 78 79 80 public ServerURL url; 81 82 public InvocationImpl( 83 PortalServer container, 84 HttpServletRequest req, 85 HttpServletResponse resp, 86 int nonSecurePort, 87 int securePort) 88 { 89 this.container = container; 90 this.req = new PortalRequestImpl(req, this); 91 this.resp = new PortalResponseImpl(resp, this); 92 this.nonSecurePort = nonSecurePort; 93 this.securePort = securePort; 94 this.interceptors = EMPTY_INTERCEPTORS; 95 this.currentInterceptor = 0; 96 this.attachments = new HashMap (); 97 this.url = null; 98 init(); 99 } 100 101 protected void init() 102 { 103 decodeParameters(); 104 decodeTarget(); 105 } 106 107 protected void decodeParameters() 108 { 109 controlParameters = new Parameters(); 110 targetParameters = new Parameters(); 111 for (Iterator i = req.getParameterMap().entrySet().iterator();i.hasNext();) 112 { 113 Map.Entry entry = (Map.Entry )i.next(); 114 String name = (String )entry.getKey(); 115 String [] values = (String [])entry.getValue(); 116 if (name.startsWith("ctrl:")) 117 { 118 controlParameters.setParameterValues(name.substring("ctrl:".length()), values); 119 } 120 else 121 { 122 targetParameters.setParameterValues(name, values); 123 } 124 } 125 } 126 127 130 protected void decodeTarget() 131 { 132 String id = controlParameters.getParameter("id"); 133 log.debug("Incoming request with id " + id); 134 if (id != null) 135 { 136 try 137 { 138 ServerObjectID objectID = (ServerObjectID)ServerObjectIDConverter.instance.toObject(id); 139 target = container.getObject(objectID); 140 } 141 catch (ConversionException e) 142 { 143 log.warn("Invalid id " + id + " use null instead", e); 144 } 145 } 146 147 if (target != null) 149 { 150 Map controlMap = controlParameters.getParameterMap(); 152 Map targetMap = targetParameters.getParameterMap(); 153 boolean secure = req.isSecure(); 154 155 ServerURL tmp = target.createURL(); 157 Parameters urlControlMap = tmp.getControlParameters(); 158 Parameters urlTargetMap = tmp.getTargetParameters(); 159 160 urlControlMap.setParameterMap(controlMap); 162 urlTargetMap.setParameterMap(targetMap); 163 tmp.setSecure(Boolean.valueOf(secure)); 164 this.url = tmp; 165 } 166 } 167 168 public void setAttachment(AttachmentKey key, Object value) 169 { 170 if (AttachmentKey.PORTAL_REQUEST == key) 171 { 172 throw new IllegalArgumentException ("Cannot set portal request"); 173 } 174 if (AttachmentKey.PORTAL_RESPONSE == key) 175 { 176 throw new IllegalArgumentException ("Cannot set portal response"); 177 } 178 attachments.put(key, value); 179 } 180 181 public Object getAttachment(AttachmentKey key) 182 { 183 if (AttachmentKey.PORTAL_REQUEST == key) 184 { 185 return req; 186 } 187 if (AttachmentKey.PORTAL_RESPONSE == key) 188 { 189 return resp; 190 } 191 return attachments.get(key); 192 } 193 194 public void removeAttachment(AttachmentKey key) 195 { 196 if (AttachmentKey.PORTAL_REQUEST == key) 197 { 198 throw new IllegalArgumentException ("Cannot remove portal request"); 199 } 200 if (AttachmentKey.PORTAL_RESPONSE == key) 201 { 202 throw new IllegalArgumentException ("Cannot remove portal response"); 203 } 204 attachments.remove(key); 205 } 206 207 public Object invokeNext() 208 { 209 if (currentInterceptor < interceptors.length) 210 { 211 try 212 { 213 return interceptors[currentInterceptor++].invoke(this); 214 } 215 finally 216 { 217 currentInterceptor--; 218 } 219 } 220 else 221 { 222 throw new IllegalStateException ("Interceptor stack reached its end"); 223 } 224 } 225 226 public Object invokeNext(Interceptor[] newInterceptors) 227 { 228 if (newInterceptors == null) 229 { 230 throw new IllegalArgumentException ("Cannot invoke with a null interceptor stack"); 231 } 232 Interceptor[] oldInterceptors = interceptors; 233 int oldCurrentInterceptor = currentInterceptor; 234 interceptors = newInterceptors; 235 currentInterceptor = 0; 236 try 237 { 238 return invokeNext(); 239 } 240 finally 241 { 242 interceptors = oldInterceptors; 243 currentInterceptor = oldCurrentInterceptor; 244 } 245 } 246 } 247 | Popular Tags |