예제 #1
0
  /**
   * Returns an input stream pointing to a HTTP API command.
   *
   * @param command Name of the command to execute
   * @param parameters Parameters, separated by ";".
   * @param manager Reference back to business layer
   * @return
   */
  public InputStream getThumbInputStreamForMicroHTTPd(String thumb, INotifiableManager manager)
      throws FileNotFoundException {
    URLConnection uc = null;
    try {
      if (mUrlSuffix == null) {
        throw new NoSettingsException();
      }
      final URL url;
      /*if (ClientFactory.XBMC_REV > 0 && ClientFactory.XBMC_REV >= ClientFactory.THUMB_TO_VFS_REV) {
      	url = new URL(mUrlSuffix + XBMC_MICROHTTPD_VFS_BOOTSTRAP + URLEncoder.encode(thumb));
      } else {*/
      url = new URL(mUrlSuffix + XBMC_MICROHTTPD_THUMB_BOOTSTRAP + thumb + ".jpg");

      Log.i(TAG, "Preparing input stream from " + url + " for microhttpd..");
      uc = getUrlConnection(url);
      return uc.getInputStream();
    } catch (FileNotFoundException e) {
      throw e;
    } catch (MalformedURLException e) {
      manager.onError(e);
    } catch (IOException e) {
      manager.onError(e);
    } catch (NoSettingsException e) {
      manager.onError(e);
    }
    return null;
  }
예제 #2
0
  /**
   * Executes a query.
   *
   * @param command Name of the command to execute
   * @param parameters Parameters
   * @param manager Reference back to business layer
   * @return Parsed JSON object, empty object on error.
   */
  public JsonNode query(String command, JsonNode parameters, INotifiableManager manager) {
    URLConnection uc = null;
    try {
      final ObjectMapper mapper = Client.MAPPER;

      if (mUrlSuffix == null) {
        throw new NoSettingsException();
      }

      final URL url = new URL(mUrlSuffix + XBMC_JSONRPC_BOOTSTRAP);
      uc = url.openConnection();
      uc.setConnectTimeout(SOCKET_CONNECTION_TIMEOUT);
      uc.setReadTimeout(mSocketReadTimeout);
      if (authEncoded != null) {
        uc.setRequestProperty("Authorization", "Basic " + authEncoded);
      }
      uc.setRequestProperty("Content-Type", "application/json");
      uc.setDoOutput(true);

      final ObjectNode data = Client.obj().p("jsonrpc", "2.0").p("method", command).p("id", "1");
      if (parameters != null) {
        data.put("params", parameters);
      }

      final JsonFactory jsonFactory = new JsonFactory();
      final JsonGenerator jg =
          jsonFactory.createJsonGenerator(uc.getOutputStream(), JsonEncoding.UTF8);
      jg.setCodec(mapper);

      // POST data
      jg.writeTree(data);
      jg.flush();

      final JsonParser jp = jsonFactory.createJsonParser(uc.getInputStream());
      jp.setCodec(mapper);
      final JsonNode ret = jp.readValueAs(JsonNode.class);
      return ret;

    } catch (MalformedURLException e) {
      manager.onError(e);
    } catch (IOException e) {
      int responseCode = -1;
      try {
        responseCode = ((HttpURLConnection) uc).getResponseCode();
      } catch (IOException e1) {
      } // do nothing, getResponse code failed so treat as default i/o exception.
      if (uc != null && responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
        manager.onError(new HttpException(Integer.toString(HttpURLConnection.HTTP_UNAUTHORIZED)));
      } else {
        manager.onError(e);
      }
    } catch (NoSettingsException e) {
      manager.onError(e);
    }
    return new ObjectNode(null);
  }
예제 #3
0
  /**
   * Executes a query.
   *
   * @param command Name of the command to execute
   * @param parameters Parameters, separated by ";".
   * @param manager Reference back to business layer
   * @return HTTP response string.
   */
  public String query(String command, String parameters, INotifiableManager manager) {
    URLConnection uc = null;

    try {
      if (mUrlSuffix == null) {
        throw new NoSettingsException();
      }

      URL url = new URL(getUrl(command, parameters));
      uc = getUrlConnection(url);

      final String debugUrl = URLDecoder.decode(url.toString());
      Log.i(TAG, debugUrl);

      final BufferedReader in =
          new BufferedReader(new InputStreamReader(uc.getInputStream()), 8192);
      final StringBuilder response = new StringBuilder();
      String line;

      while ((line = in.readLine()) != null) {
        response.append(line);
      }
      in.close();
      return response.toString().replace("<html>", "").replace("</html>", "");
    } catch (MalformedURLException e) {
      manager.onError(e);
    } catch (IOException e) {
      int responseCode = -1;
      try {
        if (uc != null) {
          responseCode = ((HttpURLConnection) uc).getResponseCode();
        }
      } catch (IOException e1) {
      } // do nothing, getResponse code failed so treat as default i/o exception.
      if (uc != null && responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
        manager.onError(new HttpException(Integer.toString(HttpURLConnection.HTTP_UNAUTHORIZED)));
      } else {
        manager.onError(e);
      }
    } catch (NoSettingsException e) {
      manager.onError(e);
    }
    return "";
  }
예제 #4
0
 /**
  * Returns an input stream pointing to a HTTP API command.
  *
  * @param command Name of the command to execute
  * @param parameters Parameters, separated by ";".
  * @param manager Reference back to business layer
  * @return
  */
 public InputStream getThumbInputStream(
     String command, String parameters, INotifiableManager manager) {
   URLConnection uc = null;
   try {
     if (mUrlSuffix == null) {
       throw new NoSettingsException();
     }
     URL url = new URL(getUrl(command, parameters));
     uc = getUrlConnection(url);
     Log.i(TAG, "Preparing input stream from " + url);
     return uc.getInputStream();
   } catch (MalformedURLException e) {
     manager.onError(e);
   } catch (IOException e) {
     manager.onError(e);
   } catch (NoSettingsException e) {
     manager.onError(e);
   }
   return null;
 }
예제 #5
0
 public InputStream getThumbInputStream(String thumb, INotifiableManager manager)
     throws FileNotFoundException {
   URLConnection uc = null;
   try {
     if (mUrlSuffix == null) {
       throw new NoSettingsException();
     }
     URL url = new URL(thumb);
     Log.i(TAG, "Preparing input stream from " + thumb + " for microhttpd..");
     uc = getUrlConnection(url);
     return uc.getInputStream();
   } catch (FileNotFoundException e) {
     throw e;
   } catch (MalformedURLException e) {
     manager.onError(e);
   } catch (IOException e) {
     manager.onError(e);
   } catch (NoSettingsException e) {
     manager.onError(e);
   }
   return null;
 }
예제 #6
0
 public JsonNode getJson(
     INotifiableManager manager, String method, JsonNode parameters, String resultField) {
   try {
     final JsonNode response = getJson(manager, method, parameters);
     final JsonNode result = response.get(resultField);
     if (result == null) {
       throw new Exception("Could not find field \"" + resultField + "\" as return value.");
     } else {
       return response.get(resultField);
     }
   } catch (Exception e) {
     manager.onError(e);
   }
   return Client.obj();
 }
예제 #7
0
 /**
  * Executes a JSON-RPC command and returns the result as JSON object.
  *
  * @param manager Upper layer reference for error posting
  * @param method Name of the method to run
  * @param parameters Parameters of the method
  * @return Result
  */
 public JsonNode getJson(INotifiableManager manager, String method, JsonNode parameters) {
   try {
     final JsonNode response = query(method, parameters, manager);
     final JsonNode result = response.get(RESULT_FIELD);
     if (result == null) {
       if (response.get(ERROR_FIELD) == null) {
         throw new Exception("Weird JSON response, could not parse error.");
       } else {
         throw new Exception(response.get(ERROR_FIELD).get("message").getTextValue());
       }
     } else {
       return response.get(RESULT_FIELD);
     }
   } catch (Exception e) {
     manager.onError(e);
   }
   return Client.obj();
 }