001 /*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016 package org.opengion.hayabusa.servlet;
017
018 import java.io.FileInputStream;
019 import java.io.IOException;
020 import java.io.PrintWriter;
021 import java.io.UnsupportedEncodingException;
022 import java.util.Enumeration;
023 import java.util.HashMap;
024
025 import javax.servlet.ServletException;
026 import javax.servlet.ServletOutputStream;
027 import javax.servlet.http.HttpServlet;
028 import javax.servlet.http.HttpServletRequest;
029 import javax.servlet.http.HttpServletResponse;
030
031 import org.opengion.fukurou.util.Closer;
032 import org.opengion.hayabusa.common.HybsSystem;
033 import org.opengion.hayabusa.common.HybsSystemException;
034 import org.opengion.hayabusa.remote.RemoteControllable;
035
036 /**
037 * 外部からキーと値を投げて処?させるサーブレ?です?
038 * Post,Get両方に対応して?す?
039 * classキーが??です?(値はhayabusa/remote/??クラス?
040 *
041 * @og.rev 4.1.0.0 (2007/12/20) 新規作?
042 * @version 4.1
043 * @author Masakazu Takahashi
044 * @since JDK6.0,
045 *
046 */
047 public class RemoteControlServlet extends HttpServlet {
048 private static final long serialVersionUID = 542020111201L;
049 private static final String REMOTE_PKG = "org.opengion.hayabusa.remote.";
050
051 /**
052 * Postメソ?で与えられたrequestをcallClassメソ?に渡します?
053 * callClassメソ?ではclassパラメータの値を利用してクラスをロードし、??行います?
054 * 具体的な処??callClassメソ?をご覧下さ??
055 *
056 * @param request HttpServletRequestリクエス?
057 * @param response HttpServletResponseレスポンス
058 * @throws ServletException サーブレ?関係?エラーが発生した?合?throw されます?
059 * @throws IOException 入出力エラーが発生したと?
060 */
061 @Override
062 public void doPost( final HttpServletRequest request, final HttpServletResponse response ) throws ServletException, IOException {
063 callClass( request, response );
064 }
065
066 /**
067 * Getメソ?で与えられたrequestをcallClassメソ?に渡します?
068 * callClassメソ?ではclassパラメータの値を利用してクラスをロードし、??行います?
069 * 具体的な処??callClassメソ?をご覧下さ??
070 *
071 * @param request HttpServletRequestリクエス?
072 * @param response HttpServletResponseレスポンス
073 * @throws ServletException サーブレ?関係?エラーが発生した?合?throw されます?
074 * @throws IOException 入出力エラーが発生したと?
075 */
076 @Override
077 public void doGet( final HttpServletRequest request, final HttpServletResponse response ) throws ServletException, IOException {
078 callClass( request, response );
079 }
080
081 /**
082 * POSTとGETに対する実際の処??
083 * ??パラメータclassで与えられたクラス名でorg.opengion.hayabusa.remoteから
084 * クラスをロードし、MAPに格納したrequestパラメータをそのクラスに対して渡します?
085 * ロードするクラスはRemoteControllableを実?て??があります?
086 * ロードしたクラスの処?終?ると、返されたStringをresponseに出力して終?ます?
087 * なお?classパラメータがnullの場合?何もせずに終?ます?
088 *
089 * @og.rev 5.4.2.0 (2011/12/01) フォワード対?
090 *
091 * @param request リクエス?
092 * @param response レスポンス
093 * @throws ServletException サーブレ?関係?エラーが発生した?合?throw されます?
094 * @throws IOException 入出力エラーが発生したと?
095 */
096 private void callClass( final HttpServletRequest request, final HttpServletResponse response ) throws ServletException, IOException {
097 // 5.4.2.0 (2011/12/01) リクエスト?エンコードを??
098 try {
099 request.setCharacterEncoding( "UTF-8" );
100 }
101 catch ( UnsupportedEncodingException ex ) {
102 throw new HybsSystemException( ex );
103 }
104
105 // 5.4.2.0 (2011/12/01) フォワード対?
106 String file = request.getParameter( "file" );
107 if( file != null && file.length() > 0 ) {
108 response.setContentType( "application/octet-stream" );
109 // ファイル?の出?
110 FileInputStream fin = null;
111 ServletOutputStream out = null;
112 try {
113 fin = new FileInputStream( file );
114 out = response.getOutputStream();
115
116 // ファイル読み込み用バッファ
117 byte buffer[] = new byte[4096];
118 int size;
119 while((size = fin.read(buffer))!=-1) {
120 out.write(buffer,0, size);
121 out.flush();
122 }
123 }
124 finally {
125 Closer.ioClose( fin );
126 Closer.ioClose( out );
127 }
128 }
129 else {
130 String clazz = request.getParameter( "class" ); // パラメータ"class"?は???
131 if( clazz == null ) {
132 return; // 暫定??
133 }
134
135 Enumeration<?> paramEnm = request.getParameterNames(); // 4.3.3.6 (2008/11/15) Generics警告対?
136 HashMap<String,String> paramMap = new HashMap<String,String>();
137 while ( paramEnm.hasMoreElements() ) {
138 String key = ( String )( paramEnm.nextElement() );
139 paramMap.put( key, request.getParameter( key ) );
140 }
141
142 RemoteControllable rmtC = ( RemoteControllable )HybsSystem.newInstance( REMOTE_PKG + clazz );
143 String rtnString = rmtC.remoteControl( paramMap );
144 response.setContentType( "text/xml; charset=UTF-8" );
145 PrintWriter out = response.getWriter();
146 out.println( rtnString );
147 out.flush();
148 out.close();
149 }
150 }
151 }