1 28 29 package com.opencms.core; 30 31 import org.opencms.main.CmsLog; 32 import org.opencms.main.OpenCms; 33 34 import java.io.IOException ; 35 import java.io.OutputStream ; 36 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 40 50 public class CmsResponseHttpServlet implements I_CmsResponse { 51 52 private static String C_LAST_MODIFIED = "Last-Modified"; 53 54 55 private static final boolean DEBUG = false; 56 57 58 private static boolean jsdk2 = checkJsdk(); 59 60 61 private String m_contentType; 62 63 64 private OutputStream m_orgOutputStream; 65 66 67 private boolean m_redir; 68 69 70 private HttpServletRequest m_req; 71 72 73 private HttpServletResponse m_res; 74 75 76 private int m_type = com.opencms.core.I_CmsConstants.C_RESPONSE_HTTP; 77 78 84 public CmsResponseHttpServlet(HttpServletRequest req, HttpServletResponse res) { 85 86 m_res = res; 87 m_req = req; 88 m_res.setHeader("Server", "OpenCms/" + OpenCms.getSystemInfo().getVersionName()); 90 } 91 92 97 public static boolean checkJsdk() { 98 99 Class rc = HttpServletResponse .class; 101 java.lang.reflect.Method m = null; 102 try { 103 m = rc.getMethod("addHeader", new Class [] {String .class, String .class}); 104 } catch (Exception e) { 105 m = null; 106 } 107 108 boolean result = (m != null); 110 111 if (CmsLog.INIT.isDebugEnabled()) { 112 if (result) { 113 CmsLog.INIT.debug("Compatibility check - JSDK 2 detected. "); 115 } else { 116 CmsLog.INIT.debug("Compatibility check - JSDK 1 detected. "); 118 } 119 } 120 return result; 121 } 122 123 129 public void addHeader(String key, String value) { 130 131 if (DEBUG) { 132 System.err.println("CmsResponse.addHeader(" + key + "," + value + ")"); 133 } 134 135 if (jsdk2) { 136 m_res.addHeader(key, value); 137 } else { 138 m_res.setHeader(key, value); 139 } 140 } 141 142 147 public boolean containsHeader(String key) { 148 149 if (DEBUG) { 150 System.err.println("CmsResponse.containsHeader(" + key + ")"); 151 } 152 153 return m_res.containsHeader(key); 154 } 155 156 162 public String getContentType() { 163 164 if (DEBUG) { 165 System.err.println("CmsResponse.getContentType()"); 166 } 167 168 return m_contentType; 169 } 170 171 176 public HttpServletResponse getOriginalResponse() { 177 178 return m_res; 179 } 180 181 187 public int getOriginalResponseType() { 188 189 return m_type; 190 } 191 192 198 public OutputStream getOutputStream() throws IOException { 199 200 if (m_orgOutputStream == null) { 201 m_orgOutputStream = m_res.getOutputStream(); 202 } 203 return m_orgOutputStream; 204 } 205 206 211 public boolean isOutputWritten() { 212 213 return m_orgOutputStream != null; 214 } 215 216 222 223 public boolean isRedirected() { 224 225 return m_redir; 226 } 227 228 236 public void sendCmsRedirect(String location) { 237 238 if (DEBUG) { 239 System.err.println("CmsResponse.sendCmsRedirect(" + location + ")"); 240 } 241 242 String hostName = m_req.getScheme() + "://" + m_req.getServerName() + ":" + m_req.getServerPort(); 243 m_redir = true; 244 String servlet = m_req.getServletPath(); 245 String contextPath = ""; 246 try { 247 contextPath = m_req.getContextPath(); 248 } catch (NoSuchMethodError err) { 249 } 251 try { 252 m_res.sendRedirect(hostName + contextPath + servlet + location); 253 } catch (IOException exc) { 254 if (CmsLog.getLog(this).isWarnEnabled()) { 255 CmsLog.getLog(this).warn( 256 "Couldn't redirect http response to: " + hostName + contextPath + servlet + location); 257 } 258 } 259 } 260 261 268 public void sendError(int code) throws IOException { 269 270 if (DEBUG) { 271 System.err.println("CmsResponse.sendError(" + code + ")"); 272 } 273 274 m_res.sendError(code); 275 } 276 277 285 public void sendError(int code, String msg) throws IOException { 286 287 if (DEBUG) { 288 System.err.println("CmsResponse.sendError(" + code + "," + msg + ")"); 289 } 290 291 m_res.sendError(code, msg); 292 } 293 294 301 public void sendRedirect(String location) throws IOException { 302 303 if (DEBUG) { 304 System.err.println("CmsResponse.sendRedirect(" + location + ")"); 305 } 306 307 String shortLocation = location; 308 String hostName = m_req.getServerName() + ":" + m_req.getServerPort(); 309 if (shortLocation.startsWith(m_req.getScheme())) { 311 shortLocation = shortLocation.substring(m_req.getScheme().length()); 312 } 313 if (shortLocation.startsWith("://")) { 314 shortLocation = shortLocation.substring(3); 315 } 316 if (shortLocation.startsWith(hostName)) { 317 shortLocation = shortLocation.substring(hostName.length()); 318 String contextPath = ""; 319 try { 320 contextPath = m_req.getContextPath(); 321 } catch (NoSuchMethodError err) { 322 } 324 if (shortLocation.startsWith(contextPath + m_req.getServletPath())) { 325 shortLocation = shortLocation.substring((contextPath + m_req.getServletPath()).length()); 326 } 327 sendCmsRedirect(shortLocation); 328 } else { 329 330 m_res.sendRedirect(location); 332 } 333 } 334 335 340 public void setContentLength(int len) { 341 342 if (DEBUG) { 343 System.err.println("CmsResponse.setContentLength(" + len + ")"); 344 } 345 346 m_res.setContentLength(len); 347 } 348 349 354 public void setContentType(String type) { 355 356 if (DEBUG) { 357 System.err.println("CmsResponse.setContentType(" + type + ")"); 358 } 359 360 m_contentType = type; 361 m_res.setContentType(type); 362 } 363 364 370 public void setHeader(String key, String value) { 371 372 if (DEBUG) { 373 System.err.println("CmsResponse.setHeader(" + key + "," + value + ")"); 374 } 375 376 m_res.setHeader(key, value); 377 } 378 379 384 public void setLastModified(long time) { 385 386 if (DEBUG) { 387 System.err.println("CmsResponse.setLastModified(" + time + ")"); 388 } 389 390 m_res.setDateHeader(C_LAST_MODIFIED, time); 391 } 392 } | Popular Tags |