applicationservlet.java

来自「AJAX的一个好东西哦,国人的一个AJAX的实现不错的东西」· Java 代码 · 共 138 行

JAVA
138
字号
/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * 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.
 * 
 * $Id: ApplicationServlet.java 43 2005-12-12 01:24:50Z michael $
 */

package net.buffalo.web.servlet;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.buffalo.request.RequestContext;
import net.buffalo.request.RequestWorker;
import net.buffalo.request.ValidationException;
import net.buffalo.service.BuffaloWorker;
import net.buffalo.service.DefaultServiceRepository;
import net.buffalo.service.ServiceRepository;
import net.buffalo.view.ViewWorker;
import net.buffalo.web.RequestUtils;
import net.buffalo.web.upload.UploadWorker;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Buffalo application Servlet, the central servlet for all the requests.
 * 
 * @author michael
 * @version 1.2
 */
public class ApplicationServlet extends HttpServlet {

	private static final Log LOG = LogFactory.getLog(ApplicationServlet.class);

	private static final String LOCALE_COOKIE_NAME = "net.buffalo.web.locale";

	public String getServletInfo() {
		return "Buffalo Application Gateway Servlet";
	}

	/**
	 * Initialize the service, including the service object.
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		initServiceRepository();
	}

	/**
	 * initialize the service repository
	 * 
	 */
	private void initServiceRepository() {
		
		if (getServletContext().getAttribute(ServiceRepository.WEB_CONTEXT_KEY) == null) {
			LOG.info("initialize the service repository");
			ServiceRepository repository = new DefaultServiceRepository(getServletContext());
			getServletContext().setAttribute(ServiceRepository.WEB_CONTEXT_KEY, repository);
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doRequest(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doRequest(request, response);
	}

	protected void doRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		RequestContext context = null;

		context = new RequestContext(getServletContext(), request, response);
		
		String pathInfo = request.getPathInfo();
		
		LOG.debug("request path info: " + pathInfo);
		
		RequestWorker worker = null;
		
		if (pathInfo == null || pathInfo.equals("/")) {
			worker = new ViewWorker();
		} else if (pathInfo.startsWith("/view/")) {
			worker = new ViewWorker();
		} else if (pathInfo.startsWith("/buffalo/")) {
			worker = new BuffaloWorker();
		} else if (pathInfo.startsWith("/upload/")) {
			worker = new UploadWorker();
		} else {
			throw new ServletException("Cannot find the request worker!");
		}
		
		// initialize the worker
		worker.initRequest(context);
		
		try {
			worker.validate();
		} catch (ValidationException ex) {
			throw new ServletException("Service validation error", ex);
		}
		
		worker.processRequest();
		
	}

	protected Locale getLocaleFromRequest(RequestContext context)
			throws ServletException {
		Cookie cookie = context.getCookie(LOCALE_COOKIE_NAME);

		if (cookie != null)
			return RequestUtils.getLocale(cookie.getValue());

		return context.getRequest().getLocale();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?