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.util.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         * 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}