1 5 6 13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp; 14 15 import java.io.IOException ; 16 import java.io.OutputStream ; 17 import java.io.PrintWriter ; 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.Enumeration ; 22 import java.util.List ; 23 import java.util.Locale ; 24 25 import javax.portlet.PortletURL; 26 import javax.portlet.RenderResponse; 27 import javax.servlet.http.HttpServletResponse ; 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 supportedWindowState_; 36 private Collection supportedContents_; 37 private String windowId_; 38 private Input input_; 39 private Portlet portletDatas_; 40 private boolean isCurrentlySecured_; 41 private String contentType_; 42 private boolean writerAlreadyCalled_; 43 private boolean outputStreamAlreadyCalled_; 44 45 public RenderResponseImp(HttpServletResponse httpServletResponse) { 46 super(httpServletResponse); 47 } 48 49 public void fillRenderResponse(String windowId, Input input, 50 Portlet portletDatas, 51 boolean isCurrentlySecured, 52 Collection supportedContents, 53 Enumeration 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 getContentType() { 70 if(contentType_ == null || "".equals(contentType_)) 71 return null; 72 return contentType_; 73 } 74 75 public void setContentType(String contentType) { 76 if(contentType != null) 77 contentType = StringUtils.split(contentType, ';')[0]; 78 79 if(!isContentTypeSupported(contentType)){ 80 throw new IllegalArgumentException ("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 getNamespace() { 108 return windowId_.replace('-','I') + "I"; 109 } 110 111 public void setTitle(String s) { 112 ((RenderOutput) super.getOutput()).setTitle(s); 113 } 114 115 public OutputStream getPortletOutputStream() throws IOException { 116 if(writerAlreadyCalled_) 117 throw new IllegalStateException ("the output stream has already been called"); 118 if(contentType_ == null || "".equals(contentType_)) 119 throw new IllegalStateException ("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 getWriter() throws IOException { 126 if(outputStreamAlreadyCalled_) 127 throw new IllegalStateException ("the PrintWriter object has already been called"); 128 if(contentType_ == null || "".equals(contentType_)) 129 throw new IllegalStateException ("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 contentTypeToTest){ 136 Collection c = getResponseContentTypes(); 137 for (Iterator iter = c.iterator(); iter.hasNext();) { 138 String element = (String ) iter.next(); 139 if(element.equals(contentTypeToTest)){ 140 return true; 141 } 142 } 143 return false; 144 } 145 146 private Collection getResponseContentTypes() { 148 Collection c = new ArrayList (); 149 c.add(getResponseContentType()); 150 for (Iterator iter = supportedContents_.iterator(); iter.hasNext();) { 151 String element = (String ) iter.next(); 152 List l = portletDatas_.getSupports(); 153 for (int i = 0; i < l.size(); i++) { 154 Supports supportsType = (Supports) l.get(i); 155 String mimeType = supportsType.getMimeType(); 156 if(element.equals(mimeType) && !element.equals(input_.getMarkup())){ 157 List portletModes = supportsType.getPortletMode(); 158 for (Iterator iter2 = portletModes.iterator(); iter2.hasNext();) { 159 String portletMode = (String ) 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 getResponseContentType() { 172 List l = portletDatas_.getSupports(); 173 for (int i = 0; i < l.size(); i++) { 174 Supports supportsType = (Supports) l.get(i); 175 String mimeType = supportsType.getMimeType(); 176 if (mimeType.equals(input_.getMarkup())){ 177 List portletModes = supportsType.getPortletMode(); 178 for (Iterator iter = portletModes.iterator(); iter.hasNext();) { 179 String portletMode = (String ) 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 { 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 getLocale() { 216 Locale l = super.getLocale(); 217 if(l == null) 218 return new Locale ("en"); 219 return l; 220 } 221 222 } 223 | Popular Tags |