1 2 24 25 package com.lutris.appserver.server.httpPresentation.servlet; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 import java.util.Enumeration ; 30 import java.util.Stack ; 31 import java.util.StringTokenizer ; 32 33 import javax.servlet.http.Cookie ; 34 import javax.servlet.http.HttpServletRequest ; 35 36 import com.lutris.appserver.server.httpPresentation.HttpPresentationException; 37 import com.lutris.appserver.server.httpPresentation.HttpPresentationIOException; 38 import com.lutris.appserver.server.httpPresentation.HttpPresentationInputStream; 39 import com.lutris.appserver.server.httpPresentation.HttpPresentationRequest; 40 41 45 public class ServletHttpPresentationRequest 46 implements HttpPresentationRequest { 48 51 private HttpServletRequest request; 52 private HttpPresentationInputStream inputStream = null; private String presentationPath; 54 private String presentationPathInfo; 55 56 59 private boolean isRequestedSessionIdFromCookie = false; 60 61 64 private boolean isRequestedSessionIdFromUrl = false; 65 66 70 protected ServletHttpPresentationRequest(HttpServletRequest request) { 71 this.request = request; 72 StringBuffer presPath = new StringBuffer (); 73 StringBuffer presPathInfo = new StringBuffer (); 74 75 78 StringTokenizer tokens = 79 new StringTokenizer (request.getRequestURI().trim(), "/\\", false); 80 81 while (tokens.hasMoreTokens()) { 82 String name = tokens.nextToken(); 83 if (name.endsWith(".po")) { 86 presPath.append("/"); 87 presPath.append(name); 88 break; 89 } else if (name.indexOf(".po;") != -1) { 90 presPath.append("/"); 91 presPath.append(name.substring(0, name.indexOf(";"))); 92 presPathInfo.append(name.substring(name.indexOf(";"))); 93 break; 94 } else { 95 presPath.append("/"); 96 presPath.append(name); 97 } 98 } 99 if (presPath.length() == 0) { 100 presPath.append("/"); 101 } 102 103 if (tokens.hasMoreTokens()) { 104 while (tokens.hasMoreTokens()) { 106 presPathInfo.append("/"); 107 presPathInfo.append(tokens.nextToken()); 108 } 109 } 110 presentationPath = presPath.toString(); 111 presentationPathInfo = presPathInfo.toString(); 112 113 } 114 115 118 public HttpServletRequest getHttpServletRequest() { 119 return this.request; 120 } 121 122 126 public int getContentLength() throws HttpPresentationException { 127 return request.getContentLength(); 128 } 129 130 134 public String getContentType() throws HttpPresentationException { 135 return request.getContentType(); 136 } 137 138 143 public String getProtocol() throws HttpPresentationException { 144 return request.getProtocol(); 145 } 146 147 154 public String getScheme() { 155 return request.getScheme(); 156 } 157 158 162 public String getServerName() { 163 return request.getServerName(); 164 } 165 166 170 public int getServerPort() { 171 return request.getServerPort(); 172 } 173 174 178 public String getRemoteAddr() throws HttpPresentationException { 179 return request.getRemoteAddr(); 180 } 181 182 186 public String getRemoteHost() throws HttpPresentationException { 187 return request.getRemoteHost(); 188 } 189 190 200 205 206 209 public HttpPresentationInputStream getInputStream() throws HttpPresentationException { 210 if (inputStream == null) { 211 try { 212 inputStream = new ServletHttpPresentationInputStream(request.getInputStream()); 213 } catch (IOException except) { 214 throw new HttpPresentationException(new HttpPresentationIOException(except)); 218 } 219 } 220 return inputStream; 221 } 222 223 235 public String getParameter(String name) throws HttpPresentationException { 236 return request.getParameter(name); 237 } 238 239 246 public String [] getParameterValues(String name) 247 throws HttpPresentationException 248 { 249 Object values = request.getParameterValues(name); 250 if (values == null) { 251 return new String [0]; 252 } 253 if (values instanceof String ) { 254 String [] valArray = new String [1]; 255 valArray[0] = (String )values; 256 return valArray; 257 } 258 return (String [])values; 259 } 260 261 265 public Enumeration getParameterNames() throws HttpPresentationException { 266 return request.getParameterNames(); 267 } 268 269 274 public String getMethod() throws HttpPresentationException { 275 return request.getMethod(); 276 } 277 278 281 public String getRequestURI() throws HttpPresentationException { 282 return request.getRequestURI(); 283 } 284 285 288 public String getPresentationURI() throws HttpPresentationException { 289 return request.getRequestURI(); 290 } 291 292 300 public String getPresentationPath() throws HttpPresentationException { 301 return request.getContextPath(); 303 } 304 305 309 public String getPresentationObjectPath() throws HttpPresentationException { 310 return presentationPath; 311 } 312 313 314 315 322 public String getPresentationObjectRelativePath() throws HttpPresentationException { 323 String reqPathInfo = request.getPathInfo(); 324 String poPathInfo = getPathInfo(); 325 326 if (poPathInfo == null) { 328 return "/"; 329 } 330 if (poPathInfo.endsWith("/")) { 331 poPathInfo = poPathInfo.substring(0, poPathInfo.length()-1); 332 } 333 if (reqPathInfo == null) { 334 return poPathInfo; 335 } 336 if (!reqPathInfo.startsWith("/")) { 337 reqPathInfo = "/" + reqPathInfo; 338 } 339 if (reqPathInfo.endsWith("/")) { 340 reqPathInfo = reqPathInfo.substring(0,reqPathInfo.length()-1); 341 } 342 343 int pos; 344 345 if ((pos = reqPathInfo.indexOf(';')) < 0 ||reqPathInfo.endsWith(";")) { return reqPathInfo; 349 } else { 350 return reqPathInfo.substring(0, pos); 351 } 352 } 353 354 355 359 public String getApplicationPath() throws HttpPresentationException { 360 String contextPath = request.getContextPath(); 361 String servletPath = request.getServletPath(); 363 if (contextPath == null) { 364 contextPath = "/"; 365 } 366 else if (!contextPath.endsWith("/") && !servletPath.startsWith("/") ) { 368 contextPath += "/"; 369 } 370 return contextPath + servletPath; 372 373 } 374 375 380 public String getPathInfo() throws HttpPresentationException { 381 if (presentationPathInfo == null) { 382 return null; 383 } else { 384 return presentationPathInfo; 385 } 386 } 387 388 393 public String getPathTranslated() throws HttpPresentationException { 394 return request.getPathTranslated(); 395 } 396 397 401 public String getQueryString() throws HttpPresentationException { 402 return request.getQueryString(); 403 } 404 405 412 public String getRemoteUser() throws HttpPresentationException { 413 return request.getRemoteUser(); 414 } 415 416 420 public String getAuthType() throws HttpPresentationException { 421 return request.getAuthType(); 422 } 423 424 429 public Cookie [] getCookies() throws HttpPresentationException { 430 return request.getCookies(); 431 } 432 433 434 438 public void setRequestedSessionIdFromCookie(boolean isFromCookie) 439 throws HttpPresentationException { 440 isRequestedSessionIdFromCookie = isFromCookie; 441 } 442 443 444 449 public boolean isRequestedSessionIdFromCookie() 450 throws HttpPresentationException { 451 return isRequestedSessionIdFromCookie; 452 } 453 454 455 459 public void setRequestedSessionIdFromUrl(boolean isFromUrl) 460 throws HttpPresentationException { 461 isRequestedSessionIdFromUrl = isFromUrl; 462 } 463 464 465 466 471 public boolean isRequestedSessionIdFromUrl() 472 throws HttpPresentationException { 473 return isRequestedSessionIdFromUrl; 474 } 475 476 481 public String getHeader(String name) throws HttpPresentationException { 482 return request.getHeader(name); 483 } 484 485 490 public int getIntHeader(String name) throws HttpPresentationException { 491 return request.getIntHeader(name); 492 } 493 494 499 public long getDateHeader(String name) throws HttpPresentationException { 500 return request.getDateHeader(name); 501 } 502 503 508 public Enumeration getHeaderNames() throws HttpPresentationException { 509 return request.getHeaderNames(); 510 } 511 512 522 public String getAppFileURIPath(String file) 523 throws HttpPresentationException { 524 525 StringBuffer uriPath; 526 String presPath = getApplicationPath(); 527 528 if (presPath.charAt(presPath.length()-1) == '/') { 529 uriPath = new StringBuffer (presPath.substring(0, presPath.length()-1)); 530 } else { 531 uriPath = new StringBuffer (presPath); 532 } 533 534 Stack names = new Stack (); 536 File fparse = new File (file); 537 String parent; 538 while ((parent = fparse.getParent()) != null) { 539 names.push(fparse.getName()); 540 fparse = new File (parent); 541 } 542 names.push(fparse.getName()); 543 544 while (!names.empty()) { 545 uriPath.append('/'); 546 uriPath.append((String )names.pop()); 547 } 548 return uriPath.toString(); 549 } 550 551 558 public int getTotalBytes() { 559 return -1; 562 } 563 564 571 public String getPath() throws HttpPresentationException { 572 return getPresentationPath(); 573 } 574 } 575 | Popular Tags |