tohateam / google-api-translate-java

Automatically exported from code.google.com/p/google-api-translate-java
0 stars 0 forks source link

chinese xiatianQQ11602011: no proxy and servlet, I am add it, fixed #17

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
/***
     * 设置代理
     * @param szProxyServer
     * @param szPort
     */
    public static void setProxy(String szProxyServer, String szPort)
    {
        System.getProperties().put("proxySet", "true");
        System.getProperties().put("proxyHost", szProxyServer);
        System.getProperties().put("proxyPort", szPort);
    }

package com.google.api.translate;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TranslateServlet extends HttpServlet{

    // 代理信息
    private String szProxyServer = null;
    private String szPort = null;
    private String Charset = "UTF-8";

    private static final long serialVersionUID = 1L;
    public void service(HttpServletRequest request, 
HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/plain;charset=" + Charset);
        OutputStream out = response.getOutputStream();
        try {
            String szOut = Translate.translate
(request.getParameter("data"), request.getParameter("from"), 
request.getParameter("to"));
            // System.out.println(szOut);
            out.write(szOut.getBytes(Charset));
            szOut = null;
        } catch (Exception e) {
        }
        out.flush();
        out.close();
    }
    public void destroy() {
        if(null == szProxyServer || 0 == szProxyServer.trim
().length())
            System.getProperties().put("proxySet", "false");
        Charset = szProxyServer = szPort = null;
        super.destroy();
    }

    /***
     * 初始化配置并设置代理
     */
    public void init(ServletConfig config) throws ServletException
    {
        szProxyServer = config.getInitParameter("ProxyServer");
        szPort = config.getInitParameter("Port");
        if(null != config.getInitParameter("Charset"))
          Charset = config.getInitParameter("Charset");
        if(null != szProxyServer && 0 < szProxyServer.trim().length
())
        {
            Translate.setProxy(szProxyServer, szPort);
        }
        super.init(config);
    }
}

Original issue reported on code.google.com by miraclet...@gmail.com on 29 Jan 2008 at 6:10

Attachments:

GoogleCodeExporter commented 8 years ago
Thanks, but we'd prefer to leave proxy support to developers using this library
rather than integrating it into the library. To do otherwise could cause 
conflicts
with any other java code attempting to set a proxy.

Original comment by rich.mid...@gmail.com on 30 Jan 2008 at 9:04