📄 buffaloserviceservlet.java
字号:
/*
* 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: BuffaloServiceServlet.java 43 2005-12-12 01:24:50Z michael $
*/
package net.buffalo.server;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.caucho.burlap.io.BurlapInput;
import com.caucho.burlap.io.BurlapOutput;
import com.caucho.burlap.server.BurlapSkeleton;
/**
* Buffalo Service Servlet, the central servlet for the
* Buffalo Service.
*
* @author michael
* @version 1.0
* @deprecated
*/
public class BuffaloServiceServlet extends HttpServlet {
private static final long serialVersionUID = 2058268530956658665L;
private static final String BUFFALO_SERVICE_KEY =
"net.buffalo.service.BUFFALO_SERVICE";
private Object _service;
private BurlapSkeleton _skeleton;
private boolean isDebug;
public String getServletInfo() {
return "Buffalo Service Servlet";
}
/**
* Initialize the service, including the service object.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
isDebug = config.getInitParameter("debug").equals("true");
}
public synchronized void service(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
if (!req.getMethod().equals("POST")) {
res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"Buffalo Requires POST");
PrintWriter out = res.getWriter();
res.setContentType("text/html");
out.println("<h1>Buffalo Requires POST</h1>");
return;
}
String serviceName = request.getParameter(Buffalo.SERVICE_NAME_ID);
//System.out.println(serviceName);
String serviceClazz = getServiceClass(serviceName);
try {
_service = Class.forName(serviceClazz).newInstance();
if (_service instanceof BuffaloService) {
//System.out.println(_service);
((BuffaloService) _service).init(getServletContext(), request);
}
_skeleton = new BurlapSkeleton(_service);
} catch (Exception ex) {
throw new ServletException(ex);
}
InputStream is = request.getInputStream();
OutputStream os = response.getOutputStream();
BurlapInput in = new BurlapInput(is);
BurlapOutput out = new BurlapOutput(os) {
public void startReply() throws IOException {
print("<?xml version=\"1.0\" encoding=\"utf-8\"?><burlap:reply xmlns:burlap=\"http://www.amowa.net/buffalo/\">");
}
};
try {
_skeleton.invoke(in, out);
} catch (Throwable e) {
throw new ServletException(e);
}
}
/**
* Get the service class name from the buffalo config file by the
* service name.
*
* @param serviceName the service name
* @return the service class name
*
* @throws IOException
*/
private String getServiceClass(String serviceName) throws IOException {
ServiceRepository repo = null;
if (getServletContext().getAttribute(BUFFALO_SERVICE_KEY) == null) {
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("/buffalo-service.properties");
prop.load(in);
repo = BuffaloUtils.populateRepository(prop);
getServletContext().setAttribute(BUFFALO_SERVICE_KEY, repo);
} else {
repo = (ServiceRepository) (getServletContext().getAttribute(BUFFALO_SERVICE_KEY));
}
return repo.getService(serviceName);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -