KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > impl > portletAPIImp > ActionResponseImp


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5
6 /**
7  * Created by The eXo Platform SARL
8  * Author : Mestrallet Benjamin
9  * benjmestrallet@users.sourceforge.net
10  * Date: Jul 29, 2003
11  * Time: 2:44:18 PM
12  */

13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp;
14
15
16 import java.io.IOException JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
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 JavaDoc;
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 JavaDoc location;
42   private boolean sendRedirectAlreadyOccured;
43   private boolean redirectionPossible;
44   private Input input;
45   private Portlet portletDatas;
46
47   public ActionResponseImp(HttpServletResponse JavaDoc 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 JavaDoc("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 JavaDoc 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 JavaDoc("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 JavaDoc l = portletDatas.getSupports();
101     for (Iterator JavaDoc iterator = l.iterator(); iterator.hasNext();) {
102       Supports supports = (Supports) iterator.next();
103       if (input.getMarkup().equals(supports.getMimeType())) {
104         List JavaDoc modeList = supports.getPortletMode();
105         for (Iterator JavaDoc iterator1 = modeList.iterator(); iterator1.hasNext();) {
106           String JavaDoc modeString = (String JavaDoc) 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 JavaDoc map) {
121     if(map == null)
122       throw new IllegalArgumentException JavaDoc("the map given is null");
123     if(map.containsKey(null))
124       throw new IllegalArgumentException JavaDoc("the map given contains a null key");
125     Set JavaDoc keys = map.keySet();
126     for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
127       if(! (iter.next() instanceof String JavaDoc ))
128         throw new IllegalArgumentException JavaDoc("the map contains a non String key");
129     }
130     Collection JavaDoc values = map.values();
131     for (Iterator JavaDoc iter = values.iterator(); iter.hasNext();) {
132       if(! (iter.next() instanceof String JavaDoc[] ))
133         throw new IllegalArgumentException JavaDoc("the map contains a non String[] value");
134     }
135     if(sendRedirectAlreadyOccured)
136       throw new IllegalStateException JavaDoc("sendRedirect was already called");
137     redirectionPossible = false;
138     ((ActionOutput) super.getOutput()).setRenderParameters(map);
139   }
140
141   public void setRenderParameter(String JavaDoc s, String JavaDoc s1) {
142     if(s == null) throw new IllegalArgumentException JavaDoc("the key given is null");
143     if(s1 == null) throw new IllegalArgumentException JavaDoc("the value given is null");
144     if(sendRedirectAlreadyOccured)
145       throw new IllegalStateException JavaDoc("sendRedirect was already called");
146     redirectionPossible = false;
147     ((ActionOutput) super.getOutput()).setRenderParameter(s, s1);
148   }
149
150   public void setRenderParameter(String JavaDoc s, String JavaDoc[] strings) {
151     if(s == null)
152       throw new IllegalArgumentException JavaDoc("the key given is null");
153     if(strings == null)
154       throw new IllegalArgumentException JavaDoc("the value given is null");
155     if(sendRedirectAlreadyOccured)
156       throw new IllegalStateException JavaDoc("sendRedirect was already called");
157     redirectionPossible = false;
158     ((ActionOutput) super.getOutput()).setRenderParameters(s, strings);
159   }
160   
161   public void sendRedirect(String JavaDoc location)
162                     throws IOException JavaDoc,
163                            IllegalArgumentException JavaDoc,
164                            IllegalStateException JavaDoc{
165                              
166     if(!redirectionPossible)
167       throw new IllegalStateException JavaDoc(" 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 JavaDoc ("a relative or incorrect path URL is given");
176     }
177   }
178   
179   public String JavaDoc getLocation() {
180     return location;
181   }
182
183   public boolean isSendRedirectAlreadyOccured() {
184     return sendRedirectAlreadyOccured;
185   }
186 }
187
Popular Tags