⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httptransportagent.java

📁 手机与服务器通过SyncML进行同步的客户端框架以及API
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2003-2007 Funambol
 *
 * 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
 */
package com.funambol.syncml.spds;

import com.funambol.util.StreamReaderFactory;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.io.UnsupportedEncodingException;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
import javax.microedition.io.ConnectionNotFoundException;

import com.funambol.util.Log;

/**
 *  Represents a HTTP client implementation
 **/
public final class HttpTransportAgent implements TransportAgent {

    // --------------------------------------------------------------- Constants
    private static final String PROP_MICROEDITION_CONFIGURATION =
            "microedition.configuration";
    private static final String PROP_CONTENT_LANGUAGE           =
            "Content-Language";
    private static final String PROP_CONTENT_LENGTH             =
            "Content-Length";
    private static final String PROP_CONTENT_TYPE               =
            "Content-Type";
    private static final String PROP_MICROEDITION_LOCALE        =
            "microedition.locale";
    private static final String PROP_MICROEDITION_PROFILES      =
            "microedition.profiles";
    private static final String PROP_USER_AGENT                 =
            "User-Agent";

    // specific property to send device identity to the server
    private static final String PROP_DEVICE_AGENT               =
            "Device-Agent";

    // Proprietary http header to avoid bugs of Nokia S60 3ed. FP1
    // It forces the server to set a 'Set-Cookie' header not empty
    private static final String PROP_FORCE_COOKIES              =
            "x-funambol-force-cookies";
    private static final String PROP_ACCEPT_ENCODING = "Accept-Encoding";
    private static final String PROP_CONTENT_ENCODING = "Content-Encoding";
    private static final String PROP_DATE = "Date";
    private static final String PROP_SIZE_THRESHOLD =
            "Size-Threshold";
    private static final String COMPRESSION_TYPE_GZIP = "gzip";
    private static final String COMPRESSION_TYPE_ZLIB = "deflate";

    // ------------------------------------------------------------ Private Data
    private final String userAgent;
    private String requestURL;
    private final String charset;
    private String contentType;

    // Compression parameters
    private int sizeThreshold;
    private boolean enableCompression;

    private String responseDate;

    private boolean forceCookies;

    private int NUM_RETRY = 3;

    // ------------------------------------------------------------ Constructors

    /**
     * Initialize a new HttpTransportAgent with a URL only
     * The default userAgent and charset will be used.
     *
     * @param requestURL must be non-null.
     *
     */
    public HttpTransportAgent(String requestURL, boolean compress, boolean forceCookies) {
        this(requestURL, null, null, compress, forceCookies);
    }

    /**
     * Initialize a new HttpTransportAgent with a URL and a userAgent string.
     * The default charset will be used.
     *
     * @param requestURL must be non-null
     * @param userAgent a string to be used as userAgent.
     */
    public HttpTransportAgent(String requestURL, final String userAgent,
            boolean compress, boolean forceCookies) {
        this(requestURL, userAgent, null, compress, forceCookies);
    }

    /**
     * Initialize a new HttpTransportAgent with a URL and a charset to use.
     *
     * @param requestURL must be non-null
     * @param userAgent a string to be used as userAgent.
     * @param charset a valid charset, the device charset is used by default.
     *
     */
    public HttpTransportAgent(String requestURL,
            final String userAgent,
            final String charset, boolean compress, boolean forceCookies) {

        if (requestURL == null) {
            throw new NullPointerException(
                    "TransportAgent: request URL parameter is null");
        }
        this.userAgent = userAgent;
        this.requestURL = requestURL ;
        this.charset = charset ;
        this.sizeThreshold = 0;
        this.enableCompression = compress;
        this.responseDate = null;
        this.forceCookies = forceCookies;
        Log.info("HttpTransportAgent - enableCompression: " + enableCompression);
        Log.info("HttpTransportAgent - forceCookies: " + forceCookies);
    }


    // ---------------------------------------------------------- Public methods

    /**
     * Send a message using the default charset.
     *
     * @param requestURL must be non-null
     * @param charset a valid charset, UTF-8 is used by default.
     *
     */
    public String sendMessage(String request) throws SyncException {
        return sendMessage(request, this.charset);
    }

    public String sendMessage(String request, String charset)
    throws SyncException {
        byte[] indata = null;
        byte[] outdata = null;

        if(charset != null) {
            try {
                indata = request.getBytes(charset);
            } catch(UnsupportedEncodingException uee){
                Log.error("Charset "+charset+" not supported. Using default");
                charset = null;
                indata = request.getBytes();
            }
        } else {
            indata = request.getBytes();
        }

        request = null;
        outdata = sendMessage(indata);

        indata = null;

        if (outdata==null) {
            String msg = "Response data null";
            Log.error("[sendMessage] " + msg);
            throw new SyncException(SyncException.ACCESS_ERROR, msg);
        } else {

            if(charset != null) {
                try {
                    return new String(outdata, charset);
                } catch(UnsupportedEncodingException uee){
                    Log.error("Charset "+charset+" not supported. Using default");
                    charset = null;
                    return new String(outdata);
                }
            } else {
                return new String(outdata);
            }
        }
    }

    /**
     *
     * @param request HTTP request
     * @return HTTP response
     */
    public byte[] sendMessage(byte[] request) throws SyncException {
        HttpConnection c  = null;
        InputStream    is = null;
        OutputStream   os = null;

        byte[] data = null;
        for (int i=0; i<NUM_RETRY; i++) {
            try {
                //#ifdef low_mem
//#                 System.gc();        //XXX
//#                 try { Thread.sleep(1); } catch (InterruptedException ex) {
//#                     Log.error(this, "interruptedException in sendMessage");
//#                     ex.printStackTrace();
//#                 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -