massbank.CallCgi.java Source code

Java tutorial

Introduction

Here is the source code for massbank.CallCgi.java

Source

/*******************************************************************************
 *
 * Copyright (C) 2008 JST-BIRD MassBank
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *******************************************************************************
 *
 * CGI?sXbhNX
 *
 * ver 1.0.8 2011.07.25
 *
 ******************************************************************************/
package massbank;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.ServletContext;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

public class CallCgi extends Thread {
    private Hashtable m_params = null;

    public String result = "";
    private String m_url = "";
    private ServletContext m_context;
    private int m_timeout;

    /**
     * RXgN^
     */
    public CallCgi(String url, Hashtable params, int timeout, ServletContext context) {
        this.m_url = url;
        this.m_timeout = timeout;
        this.m_context = context;
        this.m_params = params;
    }

    public void run() {
        String progName = "CallCgi";
        String msg = "";

        HttpClient client = new HttpClient();
        // ^CAEgl(msec)Zbg
        client.setTimeout(m_timeout * 1000);
        PostMethod method = new PostMethod(this.m_url);
        String strParam = "";
        if (m_params != null && m_params.size() > 0) {
            for (Enumeration keys = m_params.keys(); keys.hasMoreElements();) {
                String key = (String) keys.nextElement();
                if (!key.equals("inst_grp") && !key.equals("inst") && !key.equals("ms")
                        && !key.equals("inst_grp_adv") && !key.equals("inst_adv") && !key.equals("ms_adv")) {
                    // L?[InstrumentType,MSTypeO??Stringp??[^
                    String val = (String) m_params.get(key);
                    strParam += key + "=" + val + "&";
                    method.addParameter(key, val);
                } else {
                    // L?[InstrumentType,MSType??Stringzp??[^
                    String[] vals = (String[]) m_params.get(key);
                    for (int i = 0; i < vals.length; i++) {
                        strParam += key + "=" + vals[i] + "&";
                        method.addParameter(key, vals[i]);
                    }
                }
            }
            strParam = strParam.substring(0, strParam.length() - 1);
        }

        try {
            // ?s
            int statusCode = client.executeMethod(method);
            // Xe?[^XR?[h`FbN
            if (statusCode != HttpStatus.SC_OK) {
                // G?[
                msg = method.getStatusLine().toString() + "\n" + "URL  : " + this.m_url;
                msg += "\nPARAM : " + strParam;
                MassBankLog.ErrorLog(progName, msg, m_context);
                return;
            }
            // X|X
            //         this.result = method.getResponseBodyAsString();

            /**
             * modification start
             * Use method.getResponseBodyAsStream() rather 
             * than method.getResponseBodyAsString() (marked as deprecated) for updated HttpClient library.
             * Prevents logging of message:
             * "Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended."
             */

            String charset = method.getResponseCharSet();
            InputStream is = method.getResponseBodyAsStream();
            StringBuilder sb = new StringBuilder();
            String line = "";
            if (is != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
                while ((line = reader.readLine()) != null) {
                    reader.mark(2000);
                    String forward = reader.readLine();
                    if ((line.equals("") || line.equals("\n") || line.equals("OK")) && forward == null)
                        sb.append(line); // append last line to StringBuilder
                    else if (forward != null)
                        sb.append(line).append("\n"); // append current line with explicit line break
                    else
                        sb.append(line);

                    reader.reset();
                }
                reader.close();
                is.close();

                //            this.result = sb.toString().trim();
                this.result = sb.toString(); // trim() deleted. because the last [\t] and [\n] are removed.
                if (this.result.endsWith("\n")) // remove trailing line break
                {
                    int pos = this.result.lastIndexOf("\n");
                    this.result = this.result.substring(0, pos);
                }
            } else {
                this.result = "";
            }

            /**
             * modification end
             */
        } catch (Exception e) {
            // G?[
            msg = e.toString() + "\n" + "URL  : " + this.m_url;
            msg += "\nPARAM : " + strParam;
            MassBankLog.ErrorLog(progName, msg, m_context);
        } finally {
            // RlNV
            method.releaseConnection();
        }
    }
}