/**
   * Sends a ping.
   *
   * @param event
   * @param user
   * @return The ping response code.
   */
  protected int sendPing(AnalyticsEvent event, IAnalyticsUser user) {
    HttpURLConnection connection = null;
    DataOutputStream output = null;

    try {
      URL url = new URL(getAnalyticsURL());
      connection = (HttpURLConnection) url.openConnection();
      if (user != null) {
        connection.setRequestProperty(
            "Cookie", user.getCookie() + "; uid=" + user.getGUID()); // $NON-NLS-1$ //$NON-NLS-2$
      }
      connection.setRequestProperty("User-Agent", AnalyticsEvent.getUserAgent()); // $NON-NLS-1$
      connection.setDoOutput(true);
      connection.setReadTimeout(getTimeout());
      connection.setConnectTimeout(getTimeout());

      connection.setRequestMethod("POST"); // $NON-NLS-1$
      // writes POST
      output = new DataOutputStream(connection.getOutputStream());
      String data = event.getEventString();
      output.writeBytes(data);
      output.flush();

      if (IdeLog.isTraceEnabled(UsagePlugin.getDefault(), IDebugScopes.USAGE)) {
        IdeLog.logTrace(
            UsagePlugin.getDefault(),
            MessageFormat.format("Sending usage: {0}, {1}", url, data)); // $NON-NLS-1$
      }

      int code = connection.getResponseCode();
      if (code == HttpURLConnection.HTTP_UNAUTHORIZED || code == HttpURLConnection.HTTP_FORBIDDEN) {
        UsagePlugin.logError(
            MessageFormat.format(
                Messages.StudioAnalytics_connection_unauthorized, Integer.toString(code)));
      } else if (code < 200 || code > 205) {
        UsagePlugin.logError(
            MessageFormat.format(
                Messages.StudioAnalytics_connection_failed, Integer.toString(code)));
      }

      return code;
    } catch (Exception e) {
      UsagePlugin.logError(e);
      return HttpURLConnection.HTTP_UNAVAILABLE;
    } finally {
      if (output != null) {
        try {
          output.close();
        } catch (IOException ignore) {
        }
      }
      if (connection != null) {
        connection.disconnect();
      }
    }
  }
예제 #2
0
 private void sendEvent(AnalyticsEvent featureEvent) {
   UsagePlugin plugin = UsagePlugin.getDefault();
   if (plugin == null) {
     return;
   }
   IStudioAnalytics analytics = plugin.getStudioAnalytics();
   if (analytics == null) {
     return;
   }
   analytics.sendEvent(featureEvent);
 }