1 5 6 13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp; 14 15 16 import java.io.IOException ; 17 import java.util.Collection ; 18 import java.util.Enumeration ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import javax.portlet.ActionResponse; 25 import javax.portlet.PortletMode; 26 import javax.portlet.PortletModeException; 27 import javax.portlet.WindowState; 28 import javax.portlet.WindowStateException; 29 import javax.servlet.http.HttpServletResponse ; 30 import org.exoplatform.container.PortalContainer; 31 import org.exoplatform.services.portletcontainer.impl.PortletContainerConf; 32 import org.exoplatform.services.portletcontainer.pci.ActionOutput; 33 import org.exoplatform.services.portletcontainer.pci.Input; 34 import org.exoplatform.services.portletcontainer.pci.model.Portlet; 35 import org.exoplatform.services.portletcontainer.pci.model.Supports; 36 37 38 public class ActionResponseImp extends PortletResponseImp 39 implements ActionResponse { 40 41 private String location; 42 private boolean sendRedirectAlreadyOccured; 43 private boolean redirectionPossible; 44 private Input input; 45 private Portlet portletDatas; 46 47 public ActionResponseImp(HttpServletResponse httpServletResponse) { 48 super(httpServletResponse); 49 } 50 51 public void fillActionResponse(Input input, Portlet portletDatas){ 52 this.portletDatas = portletDatas; 53 this.input = input; 54 this.redirectionPossible = true; 55 this.sendRedirectAlreadyOccured = false; 56 } 57 58 public void emptyActionResponse() { 59 this.redirectionPossible = false; 60 this.sendRedirectAlreadyOccured = false; 61 } 62 63 public void setWindowState(WindowState windowState) throws WindowStateException { 64 if(sendRedirectAlreadyOccured) 65 throw new IllegalStateException ("sendRedirect was already called"); 66 if(windowState == null){ 67 throw new WindowStateException("The portlet mode is null", windowState); 68 } 69 if (windowState == WindowState.NORMAL || windowState == WindowState.MINIMIZED || 70 windowState == WindowState.MAXIMIZED) { 71 ((ActionOutput) super.getOutput()).setNextState(windowState); 72 redirectionPossible = false; 73 return; 74 } 75 PortalContainer manager = PortalContainer.getInstance(); 76 Enumeration e = 77 ((PortletContainerConf)manager.getComponentInstanceOfType(PortletContainerConf.class)). 78 getSupportedWindowStates(); 79 while (e.hasMoreElements()) { 80 WindowState state = (WindowState) e.nextElement(); 81 if (state.toString().equals(windowState.toString())) { 82 ((ActionOutput) super.getOutput()).setNextState(windowState); 83 redirectionPossible = false; 84 return; 85 } 86 } 87 throw new WindowStateException("The window state " + windowState.toString() + " is not supported by the portlet container", windowState); 88 } 89 90 public void setPortletMode(PortletMode portletMode) throws PortletModeException { 91 if(sendRedirectAlreadyOccured) 92 throw new IllegalStateException ("sendRedirect was already called"); 93 if(portletMode == null) 94 throw new PortletModeException("The portlet mode is null", portletMode); 95 if (portletMode == PortletMode.VIEW) { 96 ((ActionOutput) super.getOutput()).setNextMode(portletMode); 97 redirectionPossible = false; 98 return; 99 } 100 List l = portletDatas.getSupports(); 101 for (Iterator iterator = l.iterator(); iterator.hasNext();) { 102 Supports supports = (Supports) iterator.next(); 103 if (input.getMarkup().equals(supports.getMimeType())) { 104 List modeList = supports.getPortletMode(); 105 for (Iterator iterator1 = modeList.iterator(); iterator1.hasNext();) { 106 String modeString = (String ) iterator1.next() ; 107 modeString = modeString.toLowerCase() ; 108 if (modeString != null && modeString.equals(portletMode.toString())) { 109 ((ActionOutput) super.getOutput()).setNextMode(portletMode); 110 redirectionPossible = false; 111 return; 112 } 113 } 114 } 115 } 116 throw new PortletModeException("The mode " + portletMode.toString() + " is not supported by that portlet", 117 portletMode); 118 } 119 120 public void setRenderParameters(Map map) { 121 if(map == null) 122 throw new IllegalArgumentException ("the map given is null"); 123 if(map.containsKey(null)) 124 throw new IllegalArgumentException ("the map given contains a null key"); 125 Set keys = map.keySet(); 126 for (Iterator iter = keys.iterator(); iter.hasNext();) { 127 if(! (iter.next() instanceof String )) 128 throw new IllegalArgumentException ("the map contains a non String key"); 129 } 130 Collection values = map.values(); 131 for (Iterator iter = values.iterator(); iter.hasNext();) { 132 if(! (iter.next() instanceof String [] )) 133 throw new IllegalArgumentException ("the map contains a non String[] value"); 134 } 135 if(sendRedirectAlreadyOccured) 136 throw new IllegalStateException ("sendRedirect was already called"); 137 redirectionPossible = false; 138 ((ActionOutput) super.getOutput()).setRenderParameters(map); 139 } 140 141 public void setRenderParameter(String s, String s1) { 142 if(s == null) throw new IllegalArgumentException ("the key given is null"); 143 if(s1 == null) throw new IllegalArgumentException ("the value given is null"); 144 if(sendRedirectAlreadyOccured) 145 throw new IllegalStateException ("sendRedirect was already called"); 146 redirectionPossible = false; 147 ((ActionOutput) super.getOutput()).setRenderParameter(s, s1); 148 } 149 150 public void setRenderParameter(String s, String [] strings) { 151 if(s == null) 152 throw new IllegalArgumentException ("the key given is null"); 153 if(strings == null) 154 throw new IllegalArgumentException ("the value given is null"); 155 if(sendRedirectAlreadyOccured) 156 throw new IllegalStateException ("sendRedirect was already called"); 157 redirectionPossible = false; 158 ((ActionOutput) super.getOutput()).setRenderParameters(s, strings); 159 } 160 161 public void sendRedirect(String location) 162 throws IOException , 163 IllegalArgumentException , 164 IllegalStateException { 165 166 if(!redirectionPossible) 167 throw new IllegalStateException (" The sendRedirect method can not be invoked " + 168 "after any of the following methods of the ActionResponse interface has " + 169 "been called: setPortletMode, setWindowState, setRenderParameter, " + 170 "setRenderParameters"); 171 if(location.startsWith("/") || location.startsWith("http://")){ 172 this.sendRedirectAlreadyOccured = true; 173 this.location = location; 174 } else { 175 throw new IllegalArgumentException ("a relative or incorrect path URL is given"); 176 } 177 } 178 179 public String getLocation() { 180 return location; 181 } 182 183 public boolean isSendRedirectAlreadyOccured() { 184 return sendRedirectAlreadyOccured; 185 } 186 } 187 | Popular Tags |