Example #1
0
  private void checkHeaders(HTTPMethod method) {
    if (debugHeaders) {
      System.out.println("\nOpenConnection Headers for " + method.getPath());
      System.out.println("Status Line: " + method.getStatusLine());
    }

    Header[] responseHeaders = method.getResponseHeaders();
    for (int i1 = 0; i1 < responseHeaders.length; i1++) {
      Header responseHeader = responseHeaders[i1];
      if (debugHeaders) System.out.print("  " + responseHeader);
      String key = responseHeader.getName();
      String value = responseHeader.getValue();

      if (key.equals("Last-Modified")) {
        lastModified = value;
        if (debugHeaders) System.out.println(" **found lastModified = " + lastModified);

      } else if (key.equals("X-Last-Extended")) {
        lastExtended = value;
        if (debugHeaders) System.out.println(" **found lastExtended = " + lastExtended);

      } else if (key.equals("X-Last-Modified-Invalid")) {
        lastModifiedInvalid = value;
        if (debugHeaders)
          System.out.println(" **found lastModifiedInvalid = " + lastModifiedInvalid);
      }
    }

    if (debugHeaders) System.out.println("OpenConnection Headers for " + method.getPath());

    Cookie[] cookies = _session.getCookies();

    if (cookies.length > 0) {
      if (debugHeaders) System.out.println("Cookies= ");

      for (int i = 0; i < cookies.length; i++) {
        Cookie cooky = cookies[i];
        if (debugHeaders) System.out.println("  " + cooky);
        if (cooky.getName().equalsIgnoreCase("jsessionid")) hasSession = true;
      }
    }
  }
Example #2
0
  /**
   * * a simple request WILL NOT process any data received, you'll have to do that.
   *
   * @return
   */
  public HttpURLConnection simpleRequest(Skype usr) {
    try {

      URL obj = new URL(url);
      con = (HttpURLConnection) obj.openConnection();
      con.setRequestMethod(
          (type == RequestType.GET
              ? "GET"
              : (type == RequestType.POST
                  ? "POST"
                  : (type == RequestType.PUT
                      ? "PUT"
                      : (type == RequestType.DELETE ? "DELETE" : "OPTIONS")))));

      con.setRequestProperty(
          "Content-Type", isForm ? "application/x-www-form-urlencoded" : "application/json");
      con.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length));
      // con.setRequestProperty("User-Agent", "JavaSkypeProject");
      con.setRequestProperty("Cookie", api.cookies);
      con.setDoOutput(true);
      addLogin(usr);
      for (Header s : headers) {
        con.addRequestProperty(s.getType(), s.getData());
      }
      if (!(data.getBytes().length == 0)) {
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.write(data.getBytes());
        wr.flush();
        wr.close();
      }
      return con;
    } catch (Exception e) {
      System.out.println("================================================");
      System.out.println("========Unable to request  the skype api========");
      System.out.println("================================================");
      return null;
    }
  }
Example #3
0
  public String makeRequest(Skype usr) {
    try {
      URL obj = new URL(url);
      con = (HttpURLConnection) obj.openConnection();
      con.setRequestMethod(
          (type == RequestType.GET
              ? "GET"
              : (type == RequestType.POST
                  ? "POST"
                  : (type == RequestType.PUT
                      ? "PUT"
                      : (type == RequestType.DELETE ? "DELETE" : "OPTIONS")))));

      con.setRequestProperty(
          "Content-Type",
          isForm
              ? "application/x-www-form-urlencoded"
              : (file ? "application/octet-stream" : "application/json; charset=utf-8"));
      con.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length));
      con.setRequestProperty("User-Agent", "0/7.7.0.103// libhttpX.X");
      con.setRequestProperty("Cookie", api.cookies);
      con.setDoOutput(true);
      if (sendLoginHeaders) addLogin(usr);
      for (Header s : headers) {
        con.addRequestProperty(s.getType(), s.getData());
      }
      if (!(data.getBytes().length == 0)) {
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.write(data.getBytes());
        wr.flush();
        wr.close();
      }
      code = con.getResponseCode();
      if (code == 200 || code == 201) {
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
          response.append(inputLine);
        }
        in.close();
        return response.toString() == null ? "" : response.toString();

      } else if (code == 401) {
        System.out.println("\n\nBad login...");
        System.out.println(
            this.url
                + " returned 401. \nHave you been running jSkype for more than 2 days?\nWithin 4 seconds the ping-er should relog you in.\n\n");
        return "---";
      } else if (code == 204) {
        return "";
      } else {
        if (code == 404 && url.toLowerCase().contains("endpoint")) {
          System.out.println("Lost connection to skype.\nReloggin!");
          api.getSkype().relog();
          api.getPoller().prepare();
        }
        if (api.isDebugMode()) {
          if (!api.isStfuMode()) {
            // GetProfile will handle the debugging info
            if (url.equals("https://api.skype.com/users/self/contacts/profiles")) return null;
            // Debug info
            System.out.println(
                "Error contacting skype\nUrl: " + url + "\nCode: " + code + "\nData: " + data);
            for (Header header : headers) {
              System.out.println(header.getType() + ": " + header.getData());
            }
          }
        }
        return null;
      }
    } catch (UnknownHostException | SocketException e) {
      return null;
    } catch (Exception e) {
      if (api.displayInfoMessages()) {
        System.out.println("================================================");
        System.out.println("========Unable to request  the skype api========");
        System.out.println("================================================");
        System.out.println("Error: " + e.getMessage());
        System.out.println("URL: " + url);
      }
      e.printStackTrace();
      return null;
    }
  }
Example #4
0
  /**
   * Open a connection to the DODS server.
   *
   * @param urlString the URL to open.
   * @param command execute this command on the input stream
   * @throws IOException if an IO exception occurred.
   * @throws DAP2Exception if the DODS server returned an error.
   * @throws ParseException is cant parse the return
   */
  private void openConnection(String urlString, Command command)
      throws IOException, DAP2Exception, ParseException {
    HTTPMethod method = null;
    InputStream is = null;

    initSession();

    try {

      method = _session.newMethodGet(urlString);

      if (acceptCompress) method.setRequestHeader("Accept-Encoding", "deflate,gzip");

      // enable sessions
      if (allowSessions) method.setRequestHeader("X-Accept-Session", "true");

      // Execute the method.
      int statusCode = method.execute();

      // debug
      // if (debugHeaders) ucar.nc2.util.net.HttpClientManager.showHttpRequestInfo(f, method);

      if (statusCode == HTTPSession.SC_NOT_FOUND) {
        throw new DAP2Exception(DAP2Exception.NO_SUCH_FILE, method.getStatusText());
      }

      if (statusCode == HTTPSession.SC_UNAUTHORIZED) {
        throw new InvalidCredentialsException(method.getStatusText());
      }

      if (statusCode != HTTPSession.SC_OK) {
        throw new DAP2Exception(
            "Method failed:" + method.getStatusText() + " on URL= " + urlString);
      }

      // Get the response body.
      is = method.getResponseAsStream();

      // check if its an error
      Header header = method.getResponseHeader("Content-Description");
      if (header != null
          && (header.getValue().equals("dods-error") || header.getValue().equals("dods_error"))) {
        // create server exception object
        DAP2Exception ds = new DAP2Exception();
        is = dumpStream(is);
        // parse the Error object from stream and throw it
        ds.parse(is);
        throw ds;
      }

      ver = new ServerVersion(method);

      checkHeaders(method);

      // check for deflator
      Header h = method.getResponseHeader("content-encoding");
      String encoding = (h == null) ? null : h.getValue();
      // if (encoding != null) System.out.println("encoding= " + encoding);

      if (encoding != null && encoding.equals("deflate")) {
        is = new BufferedInputStream(new InflaterInputStream(is), 1000);

      } else if (encoding != null && encoding.equals("gzip")) {
        is = new BufferedInputStream(new GZIPInputStream(is), 1000);
      }

      command.process(is);

    } catch (Exception e) {
      e.printStackTrace();
      throw new DAP2Exception(e);

    } finally {
      // Release the connection.
      if (method != null) method.close();
    }
  }