Android Open Source - RealtimeMessaging-Android Rest Webservice






From Project

Back to project page RealtimeMessaging-Android.

License

The source code is released under:

MIT License

If you think the Android project RealtimeMessaging-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ibt.ortc.api;
//from  w w  w. j a  v a2 s  .c o  m
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import org.apache.http.HttpException;

public class RestWebservice {
  
  protected static void getAsync(URL url, OnRestWebserviceResponse callback) {
    requestAsync(url, "GET", null, callback);
  }

  protected static void postAsync(URL url, String content, OnRestWebserviceResponse callback) {
    requestAsync(url, "POST", content, callback);
  }

  private static void requestAsync(final URL url, final String method, final String content, final OnRestWebserviceResponse callback) {
    Runnable task = new Runnable() {

      @Override
      public void run() {
        if (method.equals("GET")) {
          try {
            String result = "https".equals(url.getProtocol()) ? secureGetRequest(url) : unsecureGetRequest(url);
            callback.run(null, result);
          } catch (IOException error) {
            callback.run(error, null);
          } catch (HttpException error) {
            callback.run(error, null);
          }
        } else if (method.equals("POST")) {
          try {
            String result = "https".equals(url.getProtocol()) ? securePostRequest(url, content) : unsecurePostRequest(url, content);
            callback.run(null, result);
          } catch (IOException error) {
            callback.run(error, null);
          } catch (HttpException error) {
            callback.run(error, null);
          }
        } else {
          callback.run(new Exception(String.format("Invalid request method - %s", method)), null);
        }
      }
    };

    new Thread(task).start();
  }

  private static String unsecureGetRequest(URL url) throws IOException, HttpException {
    HttpURLConnection connection = null;
    String result = "";

    try {
      connection = (HttpURLConnection) url.openConnection();
      InputStream responseBody;
      if (connection.getResponseCode() != 200 && connection.getResponseCode() != -1) {
        responseBody = connection.getErrorStream();

        result = readResponseBody(responseBody);
        throw new HttpException(result);
      } else {
        responseBody = connection.getInputStream();

        result = readResponseBody(responseBody);
      }
    } finally {
      if (connection != null) {
        connection.disconnect();
      }
    }

    return result;
  }

  // CAUSE: Prefer throwing/catching meaningful exceptions instead of
  // Exception
  private static String secureGetRequest(URL url) throws IOException, HttpException {
    HttpsURLConnection connection = null;
    StringBuilder result = new StringBuilder(16);

    // connection.setDoOutput(true);

    try {
      connection = (HttpsURLConnection) url.openConnection();
      BufferedReader rd = null;

      try {
        if (connection.getResponseCode() != 200) {
          // CAUSE: Reliance on default encoding
          rd = new BufferedReader(new InputStreamReader(connection.getErrorStream(), "UTF-8"));
          String line = rd.readLine();
          while (line != null) {            
            result.append(line);
            line = rd.readLine();
          }
          rd.close();
          throw new HttpException(result.toString());
        } else {
          rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
          String line = rd.readLine();
          while (line != null) {
            result.append(line);
            line = rd.readLine();
          }

        }
        // CAUSE: Method may fail to close stream on exception
      } finally {
        if (rd != null) {
          rd.close();
        }
      }
      // CAUSE: Method may fail to close connection on exception
    } finally {
      if (connection != null) {
        connection.disconnect();
      }
    }

    return result.toString();
  }

  private static String unsecurePostRequest(URL url, String postBody) throws IOException, HttpException {
    HttpURLConnection connection = null;
    StringBuilder result = new StringBuilder(16);

    try {
      connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod("POST");
      connection.setDoOutput(true);

      OutputStreamWriter wr = null;
      try {
        // CAUSE: Reliance on default encoding
        wr = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

        wr.write(postBody);

        wr.flush();
        // CAUSE: Method may fail to close stream on exception
      } finally {
        if (wr != null) {
          wr.close();
        }
      }

      BufferedReader rd = null;
      try {
        if (connection.getResponseCode() < 200 || connection.getResponseCode() > 299) {
          // CAUSE: Reliance on default encoding
          rd = new BufferedReader(new InputStreamReader(connection.getErrorStream(), "UTF-8"));
          String line = rd.readLine();
          while (line != null) {
            // CAUSE: Method concatenates strings using + in a loop
            result.append(line);
            line = rd.readLine();
          }

          throw new HttpException(result.toString());
        } else {
          // CAUSE: Reliance on default encoding
          rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
          String line = rd.readLine();
          while (line != null) {
            // CAUSE: Method concatenates strings using + in a loop
            result.append(line);
            line = rd.readLine();
          }
        }
        // CAUSE: Method may fail to close stream on exception
      } finally {
        if (rd != null) {
          rd.close();
        }
      }
      // CAUSE: Method may fail to close connection on exception
    } finally {
      if (connection != null) {
        connection.disconnect();
      }
    }

    return result.toString();
  }

  private static String securePostRequest(URL url, String postBody) throws IOException, HttpException {
    HttpsURLConnection connection = null;
    // TODO: specify a correct capacity
    StringBuilder result = new StringBuilder(16);

    try {
      connection = (HttpsURLConnection) url.openConnection();
      connection.setRequestMethod("POST");
      connection.setDoOutput(true);

      OutputStreamWriter wr = null;
      try {
        // CAUSE: Reliance on default encoding
        wr = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

        wr.write(postBody);

        wr.flush();
        // CAUSE: Method may fail to close stream on exception
      } finally {
        if (wr != null) {
          wr.close();
        }
      }

      BufferedReader rd = null;
      try {
        if (connection.getResponseCode() < 200 || connection.getResponseCode() > 299) {
          // CAUSE: Reliance on default encoding          
          rd = new BufferedReader(new InputStreamReader(connection.getErrorStream(), "UTF-8"));
          String line = rd.readLine();
          while (line != null) {
            // CAUSE: Method concatenates strings using + in a loop
            result.append(line);
            line = rd.readLine();
          }

          throw new HttpException(result.toString());
        } else {
          // CAUSE: Reliance on default encoding
          rd = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
          String line = rd.readLine();
          while (line != null) {
            // CAUSE: Method concatenates strings using + in a loop
            result.append(line);
            line = rd.readLine();
          }
        }
        // CAUSE: Method may fail to close stream on exception
      } finally {
        if (rd != null) {
          rd.close();
        }
      }
      // CAUSE: Method may fail to close connection on exception
    } finally {
      if (connection != null) {
        connection.disconnect();
      }
    }

    return result.toString();
  }

  private static String readResponseBody(InputStream responseBody) throws IOException {
    // TODO: specify a correct capacity
    StringBuilder result = new StringBuilder(16);

    if (responseBody != null) {
      BufferedReader rd = null;

      try {
        // CAUSE: Reliance on default encoding
        rd = new BufferedReader(new InputStreamReader(responseBody, "UTF-8"));
        String line = rd.readLine();
        // CAUSE: Assignment expressions nested inside other expressions
        while (line != null) {
          // CAUSE: Method concatenates strings using + in a loop
          result.append(line);
          line = rd.readLine();
        }
      } catch (IOException e) {
        result = new StringBuilder(e.getMessage());
      } finally {
        if (rd != null) {
          rd.close();
        }
      }
    }

    return result.toString();
  }

  private RestWebservice() {
  }
}




Java Source Code List

ibt.ortc.api.ApplicationTest.java
ibt.ortc.api.Authentication.java
ibt.ortc.api.Balancer.java
ibt.ortc.api.ChannelPermissions.java
ibt.ortc.api.InvalidBalancerServerException.java
ibt.ortc.api.OnDisablePresence.java
ibt.ortc.api.OnEnablePresence.java
ibt.ortc.api.OnPresence.java
ibt.ortc.api.OnRestWebserviceResponse.java
ibt.ortc.api.OrtcAuthenticationNotAuthorizedException.java
ibt.ortc.api.Ortc.java
ibt.ortc.api.Pair.java
ibt.ortc.api.Presence.java
ibt.ortc.api.RestWebservice.java
ibt.ortc.api.SecureWebConnections.java
ibt.ortc.api.Strings.java
ibt.ortc.extensibility.ChannelSubscription.java
ibt.ortc.extensibility.CharEscaper.java
ibt.ortc.extensibility.ConnectionProtocol.java
ibt.ortc.extensibility.DispatchedMessages.java
ibt.ortc.extensibility.EventEnum.java
ibt.ortc.extensibility.GcmOrtcBroadcastReceiver.java
ibt.ortc.extensibility.GcmOrtcIntentService.java
ibt.ortc.extensibility.GcmRegistration.java
ibt.ortc.extensibility.HeartbeatSender.java
ibt.ortc.extensibility.OnConnected.java
ibt.ortc.extensibility.OnDisconnected.java
ibt.ortc.extensibility.OnException.java
ibt.ortc.extensibility.OnMessageWithPayload.java
ibt.ortc.extensibility.OnMessage.java
ibt.ortc.extensibility.OnReconnected.java
ibt.ortc.extensibility.OnReconnecting.java
ibt.ortc.extensibility.OnSubscribed.java
ibt.ortc.extensibility.OnUnsubscribed.java
ibt.ortc.extensibility.OrtcClient.java
ibt.ortc.extensibility.OrtcFactory.java
ibt.ortc.extensibility.exception.OrtcAlreadyConnectedException.java
ibt.ortc.extensibility.exception.OrtcDoesNotHavePermissionException.java
ibt.ortc.extensibility.exception.OrtcEmptyFieldException.java
ibt.ortc.extensibility.exception.OrtcGcmException.java
ibt.ortc.extensibility.exception.OrtcInvalidCharactersException.java
ibt.ortc.extensibility.exception.OrtcMaxLengthException.java
ibt.ortc.extensibility.exception.OrtcNotConnectedException.java
ibt.ortc.extensibility.exception.OrtcNotSubscribedException.java
ibt.ortc.extensibility.exception.OrtcSubscribedException.java
ibt.ortc.ortclib.ApplicationTest.java
ibt.ortc.ortclib.MainActivity.java
ibt.ortc.ortclib.SettingsActivity.java
ibt.ortc.plugins.IbtRealtimeSJ.IbtRealtimeSJClient.java
ibt.ortc.plugins.IbtRealtimeSJ.IbtRealtimeSJFactory.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcMessage.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcOperation.java
ibt.ortc.plugins.IbtRealtimeSJ.OrtcServerErrorException.java
ibt.ortc.plugins.websocket.WebSocketConnection.java
ibt.ortc.plugins.websocket.WebSocketEventHandler.java
ibt.ortc.plugins.websocket.WebSocketException.java
ibt.ortc.plugins.websocket.WebSocketHandshake.java
ibt.ortc.plugins.websocket.WebSocketMessage.java
ibt.ortc.plugins.websocket.WebSocketReceiver.java
ibt.ortc.plugins.websocket.WebSocketSender.java
ibt.ortc.plugins.websocket.WebSocket.java