Search in sources :

Example 1 with HttpConnection

use of javax.microedition.io.HttpConnection in project J2ME-Loader by nikita36078.

the class Loader method getHttpInputStream.

/*
	 * InputStream-related helper functions
	 */
/**
 * Open a HTTP stream and check its MIME type
 *
 * @param name Resource name
 * @return a http stream and checks the MIME type
 */
private InputStream getHttpInputStream(String name) throws IOException {
    InputConnection ic = (InputConnection) Connector.open(name);
    // Content-Type is available for http and https connections
    if (ic instanceof HttpConnection) {
        HttpConnection hc = (HttpConnection) ic;
        // Check MIME type
        String type = hc.getHeaderField("Content-Type");
        if (type != null && !type.equals("application/m3g") && !type.equals("image/png") && !type.equals("image/jpeg")) {
            throw new IOException("Wrong MIME type: " + type + ".");
        }
    }
    InputStream is;
    try {
        is = ic.openInputStream();
    } finally {
        try {
            ic.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return is;
}
Also used : InputConnection(javax.microedition.io.InputConnection) HttpConnection(javax.microedition.io.HttpConnection) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with HttpConnection

use of javax.microedition.io.HttpConnection in project google-authenticator by google.

the class UpdateTask method run.

/**
 * {@inheritDoc}
 */
public void run() {
    try {
        // Visit the original download URL and read the JAD;
        // if the MIDlet-Version has changed, invoke the callback.
        String url = Build.DOWNLOAD_URL;
        String applicationVersion = getApplicationVersion();
        String userAgent = getUserAgent();
        String language = getLanguage();
        for (int redirectCount = 0; redirectCount < 10; redirectCount++) {
            HttpConnection c = null;
            InputStream s = null;
            try {
                c = connect(url);
                c.setRequestMethod(HttpConnection.GET);
                c.setRequestProperty("User-Agent", userAgent);
                c.setRequestProperty("Accept-Language", language);
                int responseCode = c.getResponseCode();
                if (responseCode == HttpConnection.HTTP_MOVED_PERM || responseCode == HttpConnection.HTTP_MOVED_TEMP) {
                    String location = c.getHeaderField("Location");
                    if (location != null) {
                        url = location;
                        continue;
                    } else {
                        throw new IOException("Location header missing");
                    }
                } else if (responseCode != HttpConnection.HTTP_OK) {
                    throw new IOException("Unexpected response code: " + responseCode);
                }
                s = c.openInputStream();
                String enc = getEncoding(c);
                Reader reader = new InputStreamReader(s, enc);
                final String version = getMIDletVersion(reader);
                if (version == null) {
                    throw new IOException("MIDlet-Version not found");
                } else if (!version.equals(applicationVersion)) {
                    Application application = Application.getApplication();
                    application.invokeLater(new Runnable() {

                        public void run() {
                            mCallback.onUpdate(version);
                        }
                    });
                } else {
                // Already running latest version
                }
            } finally {
                if (s != null) {
                    s.close();
                }
                if (c != null) {
                    c.close();
                }
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) HttpConnection(javax.microedition.io.HttpConnection) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) Application(net.rim.device.api.system.Application) IOException(java.io.IOException)

Example 3 with HttpConnection

use of javax.microedition.io.HttpConnection in project CodenameOne by codenameone.

the class BlackBerryImplementation method getHeaderFieldNames.

/**
 * @inheritDoc
 */
public String[] getHeaderFieldNames(Object connection) throws IOException {
    HttpConnection c = (HttpConnection) connection;
    Vector r = new Vector();
    int i = 0;
    String key = c.getHeaderFieldKey(i);
    while (key != null) {
        if (r.indexOf(key) < 0) {
            r.addElement(key);
        }
        i++;
        key = c.getHeaderFieldKey(i);
    }
    if (r.size() == 0) {
        return null;
    }
    String[] response = new String[r.size()];
    for (int iter = 0; iter < response.length; iter++) {
        response[iter] = (String) r.elementAt(iter);
    }
    return response;
}
Also used : HttpConnection(javax.microedition.io.HttpConnection) Vector(java.util.Vector)

Example 4 with HttpConnection

use of javax.microedition.io.HttpConnection in project CodenameOne by codenameone.

the class BlackBerryImplementation method getHeaderFields.

/**
 * @inheritDoc
 */
public String[] getHeaderFields(String name, Object connection) throws IOException {
    HttpConnection c = (HttpConnection) connection;
    Vector r = new Vector();
    int i = 0;
    while (c.getHeaderFieldKey(i) != null) {
        if (c.getHeaderFieldKey(i).equalsIgnoreCase(name)) {
            String val = c.getHeaderField(i);
            r.addElement(val);
        }
        i++;
    }
    if (r.size() == 0) {
        return null;
    }
    String[] response = new String[r.size()];
    for (int iter = 0; iter < response.length; iter++) {
        response[iter] = (String) r.elementAt(iter);
    }
    return response;
}
Also used : HttpConnection(javax.microedition.io.HttpConnection) Vector(java.util.Vector)

Example 5 with HttpConnection

use of javax.microedition.io.HttpConnection in project CodenameOne by codenameone.

the class GameCanvasImplementation method getHeaderFields.

/**
 * @inheritDoc
 */
public String[] getHeaderFields(String name, Object connection) throws IOException {
    HttpConnection c = (HttpConnection) connection;
    Vector r = new Vector();
    int i = 0;
    while (c.getHeaderFieldKey(i) != null) {
        if (c.getHeaderFieldKey(i).equalsIgnoreCase(name)) {
            String val = c.getHeaderField(i);
            // header spereated by commas
            if (name.equalsIgnoreCase("Set-Cookie")) {
                // it is not possible to simply tokenize on comma, because
                // the expiration date of each cookie contains a comma, therefore
                // we temporary remove this comma tokenize all the cookies and then
                // fixing the comma in the expiration date
                String cookies = "";
                Vector v = StringUtil.tokenizeString(val, ';');
                for (int j = 0; j < v.size(); j++) {
                    String keyval = (String) v.elementAt(j);
                    if (keyval.indexOf("expires") > -1) {
                        keyval = StringUtil.replaceAll(keyval, ",", "@__@");
                    }
                    cookies += keyval + ";";
                }
                cookies = cookies.substring(0, cookies.length() - 1);
                if (cookies.indexOf(",") > -1) {
                    Vector v2 = StringUtil.tokenizeString(cookies, ',');
                    for (int j = 0; j < v2.size(); j++) {
                        String value = (String) v2.elementAt(j);
                        value = StringUtil.replaceAll(value, "@__@", ",");
                        r.addElement(value);
                    }
                }
            } else {
                r.addElement(val);
            }
        }
        i++;
    }
    if (r.size() == 0) {
        return null;
    }
    String[] response = new String[r.size()];
    for (int iter = 0; iter < response.length; iter++) {
        response[iter] = (String) r.elementAt(iter);
    }
    return response;
}
Also used : HttpConnection(javax.microedition.io.HttpConnection) Vector(java.util.Vector)

Aggregations

HttpConnection (javax.microedition.io.HttpConnection)7 Vector (java.util.Vector)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 BufferedOutputStream (com.codename1.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 Reader (java.io.Reader)1 InputConnection (javax.microedition.io.InputConnection)1 Application (net.rim.device.api.system.Application)1