示例#1
0
  public static String sendAuthRequest(String accessCode) {
    String response = null;

    HttpsURLConnection connection = null;

    try {
      connection =
          InfrustructureHelper.createHttpMultipartConn(
              SynchronizationManager.serverUrl + "authenticate-user");

      DataOutputStream request = new DataOutputStream(connection.getOutputStream());

      request.writeBytes("--" + boundary + lineEnd);

      request.writeBytes("Content-Disposition: form-data; name=\"accessCode\"" + lineEnd);
      request.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
      request.writeBytes("Content-Length: " + SynchronizationService.accessCode + lineEnd);
      request.writeBytes(lineEnd);
      Log.i(TAG, "Sending code to server " + accessCode);
      request.writeBytes(accessCode);
      request.writeBytes(lineEnd);
      request.writeBytes("--" + boundary + lineEnd);

    } catch (IOException e) {
      e.printStackTrace();
      // TODO No Token situation
      return null;
    } catch (KeyStoreException e) {
      e.printStackTrace();
      // TODO No Token situation
      return null;
    } catch (CertificateException e) {
      e.printStackTrace();
      // TODO No Token situation
      return null;
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      // TODO No Token situation
      return null;
    } catch (KeyManagementException e) {
      e.printStackTrace();
      // TODO No Token situation
      return null;
    }

    // parse server response
    try {
      if (((HttpURLConnection) connection).getResponseCode() == 200) {
        InputStream stream = ((HttpURLConnection) connection).getInputStream();
        InputStreamReader isReader = new InputStreamReader(stream);
        BufferedReader br = new BufferedReader(isReader);
        String line;
        response = "";
        while ((line = br.readLine()) != null) {
          System.out.println(line);
          response += line;
        }
      } else {
        Log.e(TAG, "The Code was sent, but Token haven't gotten! (!= 200)");
        // TODO No Token situation
        return null;
      }
    } catch (IOException e) {
      e.printStackTrace();
      Log.e(TAG, "The Code was sent, but Token haven't gotten! (IOException)");
      // TODO No Token situation
      return null;
    }

    if (response.equals("null")) {
      return null;
    }
    return response;
  }
示例#2
0
  public static String sendSyncRequest(File allChangesInXML) {
    String response = "";
    HttpsURLConnection connection = null;
    try {
      connection =
          InfrustructureHelper.createHttpMultipartConn(
              SynchronizationManager.serverUrl + "get-tasks");
      connection.setRequestProperty("Accept-Encoding", "");
      System.setProperty("http.keepAlive", "false");

      DataOutputStream request = new DataOutputStream(connection.getOutputStream());

      request.writeBytes("--" + boundary + lineEnd);

      if (SynchronizationService.lastSyncTime != null) {
        // set initSync param
        request.writeBytes("Content-Disposition: form-data; name=\"lastSyncTime\"" + lineEnd);
        request.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
        request.writeBytes(
            "Content-Length: " + SynchronizationService.lastSyncTime.length() + lineEnd);
        request.writeBytes(lineEnd);
        request.writeBytes(SynchronizationService.lastSyncTime);
        request.writeBytes(lineEnd);
        request.writeBytes("--" + boundary + lineEnd);
      }

      // set user identity token
      if (SynchronizationService.accessToken != null) {
        request.writeBytes("Content-Disposition: form-data; name=\"accessToken\"" + lineEnd);
        request.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
        request.writeBytes(
            "Content-Length: " + SynchronizationService.accessToken.length() + lineEnd);
        request.writeBytes(lineEnd);
        Log.i(TAG, "Sending token " + SynchronizationService.accessToken);
        request.writeBytes(SynchronizationService.accessToken);
        request.writeBytes(lineEnd);
        request.writeBytes("--" + boundary + lineEnd);
      } else {
        Log.i(
            TAG,
            "NO ACCESS_TOKEN!!!"); // TODO Stop service and try to restart with new/may be old
                                   // google ACCESS CODE
        return null;
      }

      // attach file with all changes
      request.writeBytes(
          "Content-Disposition: form-data; "
              + "name=\""
              + "all_changes_xml"
              + "\""
              + "; filename=\""
              + allChangesInXML.getName()
              + "\""
              + lineEnd);
      request.writeBytes("Content-Type: text/xml" + lineEnd);
      request.writeBytes("Content-Length: " + allChangesInXML.length() + lineEnd);
      request.writeBytes(lineEnd);
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(allChangesInXML));
      byte[] buffer = new byte[(int) allChangesInXML.length()];
      bis.read(buffer);
      request.write(buffer);
      request.writeBytes(lineEnd);
      request.writeBytes("--" + boundary + lineEnd);
      bis.close();
    } catch (IOException e) {
      e.printStackTrace();
      CLog.e(TAG, "Sending sync data to server has failed", e);
      return null;
    } catch (CertificateException e) {
      e.printStackTrace();
      return null;
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      return null;
    } catch (KeyStoreException e) {
      e.printStackTrace();
      return null;
    } catch (KeyManagementException e) {
      e.printStackTrace();
      return null;
    } catch (ClassCastException e) {
      Log.e(TAG, "Probably we have a problem with internet connection");
      return null;
    }

    // parse server response
    try {
      if (((HttpsURLConnection) connection).getResponseCode() == 200) {
        InputStream stream = ((HttpURLConnection) connection).getInputStream();
        InputStreamReader isReader = new InputStreamReader(stream);
        BufferedReader br = new BufferedReader(isReader);
        String line;
        while ((line = br.readLine()) != null) {
          System.out.println(line);
          response += line;
        }
      } else {
        Log.e(TAG, "XML file was sent but error on server is occured");
        return null;
      }
    } catch (IOException e) {
      e.printStackTrace();
      Log.e(TAG, "Getting response sync data from server has failed");
      return null;
    }
    return response;
  }