?? abstractservicer.java
字號:
// Copyright 2005-2007 onetsoft.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.onetsoft.fastjsp;
import com.onetsoft.fastjsp.request.PageData;
import com.onetsoft.fastjsp.util.Constants;
import com.onetsoft.fastjsp.util.ResourceResolver;
import com.onetsoft.fastjsp.util.StringUtils;
import com.onetsoft.fastjsp.util.Util;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.SystemUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 缺省服務(wù)
*
* @author <a href="mailto:hgw@onetsoft.com">hgw</a>
* @since 2.0
*/
public abstract class AbstractServicer implements Servicer {
private final static String SCRITPS_PATH = "/com/onetsoft/fastjsp/resource/scripts";
private static Map frameworkResources = new HashMap(1); //Contains framework resources
FastJspServlet servlet = null;
HttpServletRequest request = null;
HttpServletResponse response = null;
/*
HttpServletRequest requestFacade = null; 對WAS無效,返回始終是:com.ibm.ws.webcontainer.srt.SRTServletRequest@74e074e0 ,似乎WAS不支持
HttpSevletRequestWrapper的實現(xiàn)。WAS是須實現(xiàn):.IServletRequest 嗎?將來再測。
WAS Server forward后會更改request.getRequestURL()為實際路徑的url,
如:http://www.example.com:9080/onet/onetforums/web/club/default/messages.jsp,與標(biāo)準(zhǔn)不一致。
此參數(shù)因為當(dāng)前 request.getRequestURL() 無法 facade ,不得不硬性實現(xiàn)為 page.getRequestURL() 以兼容WAS server!
*/
String rawRequestedURL = null;
Module module = null; //current fastjsp web module
String hostPath = null; //e.g:"http://www.testsite.com:8080"
String contextPath = null; //e.g:"/onet" or ""
String servletContextPath = null; //e.g:"/onet/forums"
PageParamsImpl pageParams = null;
PageModule[] layoutModules = null;
PageModule defaultPageModule = null;
StateURL stateURL = null;
LinkPool linkPool=null;
/**
* Construct servicer
*
* @param servlet
* @param request
* @param response
*/
public AbstractServicer(FastJspServlet servlet, HttpServletRequest request, HttpServletResponse response) {
this.servlet = servlet;
this.module = servlet.getModule();
this.request = request;
this.response = response;
init();
}
private void init() {
rawRequestedURL = request.getRequestURL().toString();
this.hostPath = StringUtils.substringBeforeLast(rawRequestedURL, request.getRequestURI());
this.contextPath = request.getContextPath();
if (contextPath.length() == 1 && contextPath.charAt(0) == '/')
contextPath = StringUtils.EMPTY;
if (module.moduleServletPath == null)
servletContextPath = contextPath;
else
servletContextPath = new StringBuffer().append(contextPath).append(request.getServletPath()).toString();
if (servletContextPath.length() > 0 && servletContextPath.charAt(servletContextPath.length() - 1) == '/')
servletContextPath = servletContextPath.substring(0, servletContextPath.length() - 1);
stateURL = getURLState();
/* Debug.debug(" ----------- Context info begin ------------------------");
Debug.debug("AbstractServicer.init() Line:96....hostPath:" + hostPath);
Debug.debug("AbstractServicer.init() Line:97....contextPath:" + contextPath);
Debug.debug("AbstractServicer.init() Line:98....servletContextPath:" + servletContextPath);
Debug.debug("AbstractServicer.init() Line:99....module.urlExt:" + module.urlExt);
Debug.debug("AbstractServicer.init() Line:99....raw contextPath:" + request.getContextPath());
Debug.debug("AbstractServicer.init() Line:99....raw url:" + request.getRequestURL());
Debug.debug("AbstractServicer.init() Line:99....raw uri:" + request.getRequestURI());
Debug.debug("AbstractServicer.init() Line:100 ..raw servletPath:" + request.getServletPath());
Debug.debug("AbstractServicer.init() Line:101 ..raw pathInfo:" + request.getPathInfo());
Debug.debug(" ----------- Context info end ------------------------");*/
}
/**
* 執(zhí)行當(dāng)前jsp頁面服務(wù)
*
* @throws ServletException
* @throws IOException
*/
protected void doService() throws ServletException, IOException {
try {
String pathinfo = getPathInfo(request);
if (pathinfo == null || (pathinfo.length() == 1 && pathinfo.charAt(0) == '/') || pathinfo.endsWith(module.urlExt)
|| pathinfo.indexOf('.') == -1 /*|| request.getServletPath().length() == 0*/) {
/* 獲取當(dāng)前布局 */
layoutModules = getLayoutModules();
if (layoutModules == null || layoutModules.length == 0) {
outputNoLayoutInfo(getNoLayoutInfo());
return;
}
defaultPageModule = getDefaultLayoutModule();
if (defaultPageModule == null)
defaultPageModule = layoutModules[0];
if (!ArrayUtils.contains(layoutModules, defaultPageModule)) {
outputNoLayoutInfo("Default layout\"" + defaultPageModule + "\" is not exists in the layoutModules!");
return;
}
/* 處理頁面 */
processPageService(pathinfo);
} else if (pathinfo.startsWith(StringUtils.RESOURCE_URL_PREFIX)) {
renderFrameworkReources(pathinfo);
} else {
renderContextReources(pathinfo);
}
} catch (ServletException e) {
throw e;
} catch (IOException e) {
throw e;
} catch (Exception e) {
handleException(e);
}
}
private String getPathInfo(HttpServletRequest request) {
String uri = request.getRequestURI();
uri = StringUtils.substringAfter(uri, contextPath);
String r = null;
if (module.moduleServletPath == null) {
r = uri;
} else {
if (uri != null && uri.length() > module.moduleServletPath.length()) {
r = uri.substring(module.moduleServletPath.length());
}
}
/* try {
PaeeParamsImpl中已經(jīng)基于 URLDecoder.decode(s,s)解碼,這里不再需要了。
if (r != null)
r = new String(r.getBytes(Constants.ISO_8859_1), module.charset); //編碼中文等亞洲雙字節(jié)url參數(shù)
} catch (UnsupportedEncodingException e) {
throw new ApplicationRuntimeException(e);
}*/
return r;
}
protected String getNoLayoutInfo() {
return "<b>NO WEB MODULES AVAILABLE</b>";
}
private void outputNoLayoutInfo(String info) {
Writer out = null;
try {
out = new OutputStreamWriter(response.getOutputStream(), Constants.UTF_8);
out.write(info);
out.close();
out = null;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void processPageService(String pathInfo) throws IOException, ServletException {
/*
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);*/
AbstractPageService service = null;
try {
pageParams = new PageParamsImpl(this);
pathInfo = stateURL.decodeBeforeDataBuild(pathInfo);
pageParams.init(pathInfo);
if (pageParams.layoutModule == Util.EMPTY_PAGE_MODULE) {
/*if (getLayoutModules() == null || getLayoutModules().length == 0) {
outputNoLayoutInfo(getNoLayoutInfo());
} else {*/
response.sendError(HttpServletResponse.SC_NOT_FOUND, "NO LAYOUT FOUND!");
/* }*/
return;
} else if (pageParams.pageClass == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, "NO PAGE CLASS FOUND:" + pageParams.pageClassStr);
return;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -