KickJava   Java API By Example, From Geeks To Geeks.

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


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: 5:52:27 PM
12  */

13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp;
14
15 import java.io.IOException JavaDoc;
16 import java.io.OutputStream JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import javax.portlet.PortletURL;
26 import javax.portlet.RenderResponse;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.commons.lang.StringUtils;
30 import org.exoplatform.services.portletcontainer.pci.*;
31 import org.exoplatform.services.portletcontainer.pci.model.*;
32
33
34 public class RenderResponseImp extends PortletResponseImp implements RenderResponse {
35   private Enumeration JavaDoc supportedWindowState_;
36   private Collection JavaDoc supportedContents_;
37   private String JavaDoc windowId_;
38   private Input input_;
39     private Portlet portletDatas_;
40     private boolean isCurrentlySecured_;
41   private String JavaDoc contentType_;
42   private boolean writerAlreadyCalled_;
43   private boolean outputStreamAlreadyCalled_;
44
45     public RenderResponseImp(HttpServletResponse JavaDoc httpServletResponse) {
46     super(httpServletResponse);
47   }
48
49   public void fillRenderResponse(String JavaDoc windowId, Input input,
50                                                                  Portlet portletDatas,
51                                  boolean isCurrentlySecured,
52                                  Collection JavaDoc supportedContents,
53                                  Enumeration JavaDoc supportedWindowState) {
54     this.windowId_ = windowId;
55     this.input_ = input;
56         this.portletDatas_ = portletDatas;
57         this.isCurrentlySecured_ = isCurrentlySecured;
58     this.supportedContents_ = supportedContents;
59     this.supportedWindowState_ = supportedWindowState;
60   }
61
62   public void emptyRenderResponse() {
63     this.windowId_ = null;
64     contentType_ = null;
65     writerAlreadyCalled_ = false;
66     outputStreamAlreadyCalled_ = false;
67   }
68
69   public String JavaDoc getContentType() {
70     if(contentType_ == null || "".equals(contentType_))
71       return null;
72     return contentType_;
73   }
74   
75   public void setContentType(String JavaDoc contentType) {
76     if(contentType != null)
77       contentType = StringUtils.split(contentType, ';')[0];
78     
79     if(!isContentTypeSupported(contentType)){
80       throw new IllegalArgumentException JavaDoc("the content type : " + contentType
81                                          + " is not supported.");
82     }
83     this.contentType_ = contentType;
84   }
85
86   public PortletURL createRenderURL() {
87     if(input_.getPortletURLFactory() != null) {
88         return input_.getPortletURLFactory().createPortletURL(PortletURLFactory.RENDER);
89     }
90     return new PortletURLImp(PortletURLFactory.RENDER, input_.getMarkup(),
91                                                          portletDatas_.getSupports(),
92                                                          isCurrentlySecured_, customWindowStates,
93                                                          supportedWindowState_, input_.getBaseURL(),
94                                                          input_.getWindowID());
95   }
96
97   public PortletURL createActionURL() {
98     if(input_.getPortletURLFactory() != null) {
99         return input_.getPortletURLFactory().createPortletURL(PortletURLFactory.ACTION);
100     }
101     return new PortletURLImp(PortletURLFactory.ACTION, input_.getMarkup(),
102                                                          portletDatas_.getSupports(), isCurrentlySecured_,
103                                                          customWindowStates, supportedWindowState_,
104                                                          input_.getBaseURL(),input_.getWindowID());
105   }
106
107   public String JavaDoc getNamespace() {
108     return windowId_.replace('-','I') + "I";
109   }
110
111   public void setTitle(String JavaDoc s) {
112     ((RenderOutput) super.getOutput()).setTitle(s);
113   }
114
115   public OutputStream JavaDoc getPortletOutputStream() throws IOException JavaDoc {
116     if(writerAlreadyCalled_)
117       throw new IllegalStateException JavaDoc("the output stream has already been called");
118     if(contentType_ == null || "".equals(contentType_))
119       throw new IllegalStateException JavaDoc("the content type has not been set before calling the" +
120         "getPortletOutputStream() method.");
121     outputStreamAlreadyCalled_ = true;
122     return super.getOutputStream();
123   }
124   
125   public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
126     if(outputStreamAlreadyCalled_)
127       throw new IllegalStateException JavaDoc("the PrintWriter object has already been called");
128     if(contentType_ == null || "".equals(contentType_))
129       throw new IllegalStateException JavaDoc("the content type has not been set before calling the" +
130         "getWriter() method.");
131     writerAlreadyCalled_ = true;
132     return super.getWriter();
133   }
134   
135   private boolean isContentTypeSupported(String JavaDoc contentTypeToTest){
136     Collection JavaDoc c = getResponseContentTypes();
137     for (Iterator JavaDoc iter = c.iterator(); iter.hasNext();) {
138       String JavaDoc element = (String JavaDoc) iter.next();
139       if(element.equals(contentTypeToTest)){
140         return true;
141       }
142     }
143     return false;
144   }
145
146   // TODO could be shared with PortletRequest.getResponseContentType()
147
private Collection JavaDoc getResponseContentTypes() {
148     Collection JavaDoc c = new ArrayList JavaDoc();
149     c.add(getResponseContentType());
150     for (Iterator JavaDoc iter = supportedContents_.iterator(); iter.hasNext();) {
151       String JavaDoc element = (String JavaDoc) iter.next();
152       List JavaDoc l = portletDatas_.getSupports();
153       for (int i = 0; i < l.size(); i++) {
154         Supports supportsType = (Supports) l.get(i);
155         String JavaDoc mimeType = supportsType.getMimeType();
156         if(element.equals(mimeType) && !element.equals(input_.getMarkup())){
157           List JavaDoc portletModes = supportsType.getPortletMode();
158           for (Iterator JavaDoc iter2 = portletModes.iterator(); iter2.hasNext();) {
159             String JavaDoc portletMode = (String JavaDoc) iter2.next();
160             if(portletMode.equals(input_.getPortletMode().toString())){
161               c.add(mimeType);
162               break;
163             }
164           }
165         }
166       }
167     }
168     return c;
169   }
170   
171   private String JavaDoc getResponseContentType() {
172     List JavaDoc l = portletDatas_.getSupports();
173     for (int i = 0; i < l.size(); i++) {
174       Supports supportsType = (Supports) l.get(i);
175       String JavaDoc mimeType = supportsType.getMimeType();
176       if (mimeType.equals(input_.getMarkup())){
177         List JavaDoc portletModes = supportsType.getPortletMode();
178         for (Iterator JavaDoc iter = portletModes.iterator(); iter.hasNext();) {
179           String JavaDoc portletMode = (String JavaDoc) iter.next();
180           if(portletMode.equals(input_.getPortletMode().toString())){
181             return mimeType;
182           }
183         }
184       }
185     }
186     return "text/html";
187   }
188   
189   
190   public void flushBuffer() throws IOException JavaDoc {
191     return;
192   }
193
194   public int getBufferSize() {
195     return 0;
196   }
197
198   public boolean isCommitted() {
199     return true;
200   }
201
202   public void reset() {
203     return;
204   }
205
206   public void resetBuffer() {
207     return;
208   }
209
210   public void setBufferSize(int arg0) {
211     return;
212   }
213
214
215   public Locale JavaDoc getLocale() {
216     Locale JavaDoc l = super.getLocale();
217     if(l == null)
218       return new Locale JavaDoc("en");
219     return l;
220   }
221
222 }
223
Popular Tags