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 */ 016package org.opengion.hayabusa.servlet; 017 018import java.io.FileInputStream; 019import java.io.IOException; 020import java.io.PrintWriter; 021import java.io.UnsupportedEncodingException; 022import java.util.Enumeration; 023import java.util.HashMap; 024 025import javax.servlet.ServletException; 026import javax.servlet.ServletOutputStream; 027import javax.servlet.http.HttpServlet; 028import javax.servlet.http.HttpServletRequest; 029import javax.servlet.http.HttpServletResponse; 030 031import org.opengion.fukurou.system.Closer; 032import org.opengion.hayabusa.common.HybsSystem; 033import org.opengion.hayabusa.common.HybsSystemException; 034import 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 */ 047public 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 * デフォルトコンストラクター 053 * 054 * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor. 055 */ 056 public RemoteControlServlet() { super(); } // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。 057 058 /** 059 * Postメソッドで与えられたrequestをcallClassメソッドに渡します。 060 * callClassメソッドではclassパラメータの値を利用してクラスをロードし、処理を行います。 061 * 具体的な処理はcallClassメソッドをご覧下さい。 062 * 063 * @param request HttpServletRequestリクエスト 064 * @param response HttpServletResponseレスポンス 065 * @throws ServletException サーブレット関係のエラーが発生した場合、throw されます。 066 * @throws IOException 入出力エラーが発生したとき 067 */ 068 @Override 069 public void doPost( final HttpServletRequest request, final HttpServletResponse response ) throws ServletException, IOException { 070 callClass( request, response ); 071 } 072 073 /** 074 * Getメソッドで与えられたrequestをcallClassメソッドに渡します。 075 * callClassメソッドではclassパラメータの値を利用してクラスをロードし、処理を行います。 076 * 具体的な処理はcallClassメソッドをご覧下さい。 077 * 078 * @param request HttpServletRequestリクエスト 079 * @param response HttpServletResponseレスポンス 080 * @throws ServletException サーブレット関係のエラーが発生した場合、throw されます。 081 * @throws IOException 入出力エラーが発生したとき 082 */ 083 @Override 084 public void doGet( final HttpServletRequest request, final HttpServletResponse response ) throws ServletException, IOException { 085 callClass( request, response ); 086 } 087 088 /** 089 * POSTとGETに対する実際の処理です 090 * 必須パラメータclassで与えられたクラス名でorg.opengion.hayabusa.remoteから 091 * クラスをロードし、MAPに格納したrequestパラメータをそのクラスに対して渡します。 092 * ロードするクラスはRemoteControllableを実装している必要があります。 093 * ロードしたクラスの処理が終了すると、返されたStringをresponseに出力して終了します。 094 * なお、classパラメータがnullの場合は何もせずに終了します。 095 * 096 * @og.rev 5.4.2.0 (2011/12/01) フォワード対応 097 * 098 * @param request リクエスト 099 * @param response レスポンス 100 * @throws ServletException サーブレット関係のエラーが発生した場合、throw されます。 101 * @throws IOException 入出力エラーが発生したとき 102 */ 103 private void callClass( final HttpServletRequest request, final HttpServletResponse response ) throws ServletException, IOException { 104 // 5.4.2.0 (2011/12/01) リクエストのエンコードを指定 105 try { 106 request.setCharacterEncoding( "UTF-8" ); 107 } 108 catch( final UnsupportedEncodingException ex ) { 109 throw new HybsSystemException( ex ); 110 } 111 112 // 5.4.2.0 (2011/12/01) フォワード対応 113 final String file = request.getParameter( "file" ); 114 if( file != null && file.length() > 0 ) { 115 response.setContentType( "application/octet-stream" ); 116 // ファイル内容の出力 117 FileInputStream fin = null; 118 ServletOutputStream out = null; 119 try { 120 fin = new FileInputStream( file ); 121 out = response.getOutputStream(); 122 123 // ファイル読み込み用バッファ 124 final byte buffer[] = new byte[4096]; 125 int size; 126 while((size = fin.read(buffer))!=-1) { 127 out.write(buffer,0, size); 128 out.flush(); 129 } 130 } 131 finally { 132 Closer.ioClose( fin ); 133 Closer.ioClose( out ); 134 } 135 } 136 else { 137 final String clazz = request.getParameter( "class" ); // パラメータ"class"だけは必ず必要 138 if( clazz == null ) { 139 return; // 暫定処理 140 } 141 142 final Enumeration<?> paramEnm = request.getParameterNames(); // 4.3.3.6 (2008/11/15) Generics警告対応 143 final HashMap<String,String> paramMap = new HashMap<>(); 144 while( paramEnm.hasMoreElements() ) { 145 final String key = ( String )( paramEnm.nextElement() ); 146 paramMap.put( key, request.getParameter( key ) ); 147 } 148 149 final RemoteControllable rmtC = HybsSystem.newInstance( REMOTE_PKG + clazz ); 150 final String rtnString = rmtC.remoteControl( paramMap ); 151 response.setContentType( "text/xml; charset=UTF-8" ); 152 final PrintWriter out = response.getWriter(); 153 out.println( rtnString ); 154 out.flush(); 155 out.close(); 156 } 157 } 158}