예제 #1
0
  /**
   * Stores the cookies currently saved in memory, to the record store.
   *
   * @param cookiesStorage the name of the record store to hold the cookies.
   * @throws IOException
   */
  public void storeCookies(String cookiesStorage) throws IOException {
    if (savedCookies.size() == 0) {
      Log.logInfo("No cookies to store in recordstore.");
      return; // nothing to save.
    }
    try {
      try {
        connector.rmsDelete(cookiesStorage);
      } catch (Throwable e) { // try to delete last save cookies.
        Log.logWarn("Failed to delete old saved cookies file. " + e.getMessage());
      }

      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      DataOutputStream dout = new DataOutputStream(bout);
      for (int i = 0; i < savedCookies.size(); ++i) {
        Cookie c = (Cookie) savedCookies.elementAt(i);
        Cookie.serialize(c, dout);
      }
      byte[] buf = bout.toByteArray();
      int free = connector.rmsFree();
      if (free < buf.length)
        throw new IOException("Not enough free storage space to save cookies.");
      connector.rmsWrite(cookiesStorage, buf);
    } catch (Throwable e) {
      if (e instanceof IOException) throw (IOException) e;
      else throw new IOException("Store cookies failed. " + e.getMessage());
    }
    Log.logInfo("Stored " + savedCookies.size() + " to record store: " + cookiesStorage);
  }
예제 #2
0
  /**
   * Load the cookies previously stored in the specified recordstore using the {@link
   * #storeCookies(String)} method.
   *
   * @param cookiesStorage
   * @throws IOException
   */
  public void loadCookies(String cookiesStorage) throws IOException {
    try {
      InputStream in = connector.rmsRead(cookiesStorage);
      if (in == null) return; // no stored cookies found

      DataInputStream din = new DataInputStream(in);
      int cookiesCount = 0;
      while (din.available() > 0) {
        Cookie c = Cookie.deSerialize(din);
        savedCookies.addElement(c);
        cookiesCount++;
      }
      Log.logInfo("Loaded " + cookiesCount + " cookies from record store: " + cookiesStorage);
    } catch (Throwable e) {
      if (e instanceof IOException) throw (IOException) e;
      else throw new IOException("Load cookies failed. " + e.getMessage());
    }
  }
예제 #3
0
  /**
   * Makes a http request to URL using the set method (HttpConnection.GET or HttpConnection.POST)
   * and the supplied request parameters.
   *
   * @param url
   * @param requestMethod
   * @param requestProperties User supplied Http request header parameters.
   * @param data if method is POST then the post data are in this array.
   * @param updateCurrentPage if set to true the HttpClient will remember the URL of this request in
   *     order to handle feature relative path requests
   * @return
   * @throws InterruptedIOException
   * @throws SecurityException
   * @throws IOException
   * @throws IllegalStateException
   * @throws Exception
   */
  public Response requestResource(
      String url,
      String requestMethod,
      Hashtable requestProperties,
      byte[] data,
      boolean updateCurrentPage)
      throws InterruptedIOException, SecurityException, IOException, IllegalStateException,
          Exception {
    url = getAbsolutEncodedURL(url);

    int idx = url.indexOf("://");
    if (idx == -1) throw new IllegalArgumentException("URL must start with the protocol.");

    String protocol = url.substring(0, idx);

    if (protocol.equals("file")) {
      // Log.logInfo("File: ["+url+"]");
      InputStream in = null;
      try {
        in = connector.openInputStream(url);
      } catch (Exception e) {
        Log.logDebug("Exception:" + e.getClass() + ": " + e.getMessage());
      }

      if (in == null) {
        Log.logWarn("Failed to load local resource " + url);
        return null;
      }

      Response result = new Response("", "file", url.substring(7), in);
      if (updateCurrentPage) currentResponse = result;

      return result;
    }

    if (protocol.equals("rms")) {
      // Log.logInfo("File: ["+url+"]");
      InputStream in = null;
      String rms = url.substring(6);
      try {
        in = connector.rmsRead(rms);
      } catch (Exception e) {
        Log.logDebug("Exception:" + e.getClass() + ": " + e.getMessage());
      }

      if (in == null) {
        Log.logWarn("Failed to load local resource " + url);
        return null;
      }

      Response result = new Response("", "rms", rms, in);
      if (updateCurrentPage) currentResponse = result;

      return result;
    }

    HttpConnection conn = null;
    Log.logInfo(requestMethod + " [" + url + "]");

    try { // the resource is located remotelly. Try to retrieve it.
      int mode = Connector.READ_WRITE;
      // XXX CREATING A READ ONLY CONNECTION WILL CAUSE SOME BLACKBERRY DEVICES NOT TO WORK. XXX
      // if(HttpConnection.POST.equals(requestMethod) && data!=null) mode=Connector.READ_WRITE;
      int code;
      Response result = new Response();
      registerConnection(result);

      conn = (HttpConnection) connector.open(url, mode, true);
      conn.setRequestMethod(requestMethod);
      for (int i = 0; i < defaultHeaderValues.length; ++i) {
        conn.setRequestProperty(defaultHeaderValues[i][0], defaultHeaderValues[i][1]);
      }

      if (requestProperties != null) {
        Enumeration propKeys = requestProperties.keys();
        while (propKeys.hasMoreElements()) {
          String key = (String) propKeys.nextElement();
          String val = (String) requestProperties.get(key);
          conn.setRequestProperty(key, val);
        }
      }

      // add cookies
      String cookies = getCookies(conn, conn.getFile());
      if (cookies.length() > 0) {
        conn.setRequestProperty("Cookie", cookies);
      }

      // add referer
      if (currentResponse != null && currentResponse.getProtocol().equals("file") == false) {
        String q = currentResponse.getQuery();
        if (q != null) q = "?" + q;
        else q = "";
        String referer = currentResponse.getBaseURL() + currentResponse.getFile() + q;
        conn.setRequestProperty("Referer", referer);
      }
      if (mode == Connector.READ_WRITE && data != null) // exoume na grapsoume kiolas.
      {
        conn.setRequestProperty("Content-Length", "" + data.length);
        OutputStream out = conn.openOutputStream();
        out.write(data);
        out.close();
        Log.logDebug("Post data[" + data.length + "] sent.");
      }

      Log.logDebug("Attempting to retrieve response code..");
      code = conn.getResponseCode();
      result.setConnection(conn);
      Log.logInfo("Response " + code + " " + conn.getResponseMessage());

      for (int i = 0; i < 100; ++i) {
        String key = conn.getHeaderFieldKey(i);
        if (key == null) break;

        if (key.toLowerCase().equals("set-cookie")) {
          // the cookieStr may be more than one cookies separated by commas.
          // First we split the cookies and then we parse them.
          // this is a bit tricky since a cookie may contain commas as well...(who thought of that
          // delimiter anyway?)
          Vector cookieStrings = Cookie.splitCookies(conn.getHeaderField(i));
          for (int j = 0; j < cookieStrings.size(); ++j)
            saveCookie((String) cookieStrings.elementAt(j), conn.getHost());
        } else Log.logDebug(key + ": " + conn.getHeaderField(i));
      }

      if (code == HttpConnection.HTTP_MOVED_PERM
          || code == HttpConnection.HTTP_MOVED_TEMP
          || // 301 or 307 redirect using the same method (post of get)
          code
              == HttpConnection.HTTP_SEE_OTHER) // must redirect using the GET method (see protocol)
      {
        if (updateCurrentPage) currentResponse = result;

        if (result.isCanceled()) throw new InterruptedIOException("Redirect canceled by user.");

        redirectCount++;
        String redirect = conn.getHeaderField("location");
        Log.logInfo("Redirect[" + redirectCount + "] " + code + " to location: " + redirect);
        if (redirectCount < MAX_REDIRECTS) {
          try {
            conn.close();
          } catch (IOException e) {
            Log.logWarn("HttpClient: Failed to close connection on redirect.", e);
          }

          conn = null; // make old connection null so on finally we will not try to unregister it
          // again.
          if (code == HttpConnection.HTTP_MOVED_PERM || code == HttpConnection.HTTP_MOVED_TEMP)
            return requestResource(
                redirect, requestMethod, requestProperties, data, updateCurrentPage);
          else
            return requestResource(
                redirect, HttpConnection.GET, requestProperties, data, updateCurrentPage);
        } else {
          throw new IllegalStateException("Too many redirects.");
        }
      } else // response is 200 or another http code.
      {
        if (updateCurrentPage
            && code == HttpConnection.HTTP_OK) // updateCurrentPage only when response is 200 (OK)
        currentResponse = result;

        return result;
      }
    } catch (Exception ex) {
      if (ex instanceof InterruptedIOException) Log.logInfo("USER Closed connection to " + url);
      else Log.logError("Request to " + url + " failed.", ex);

      if (conn != null) {
        try {
          conn.close();
        } catch (IOException e) {
          Log.logWarn("Failed to close the connection.", e);
        }
      }
      throw ex;
    } finally {
      redirectCount = 0;
    }
  }