📄 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;
/**
* 缺省服务
*
* @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的实现。WAS是须实现:.IServletRequest 吗?将来再测。
WAS Server forward后会更改request.getRequestURL()为实际路径的url,
如:http://www.example.com:9080/onet/onetforums/web/club/default/messages.jsp,与标准不一致。
此参数因为当前 request.getRequestURL() 无法 facade ,不得不硬性实现为 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 ------------------------");*/
}
/**
* 执行当前jsp页面服务
*
* @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*/) {
/* 获取当前布局 */
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中已经基于 URLDecoder.decode(s,s)解码,这里不再需要了。
if (r != null)
r = new String(r.getBytes(Constants.ISO_8859_1), module.charset); //编码中文等亚洲双字节url参数
} 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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -