Exemplo n.º 1
0
  // A general method for calling api and receiving response
  private String callAPI(final JSONObject param, final String api) {
    URL apiURL;
    try {

      apiURL = new URL(url + api);
      HttpURLConnection conn;

      conn = (HttpURLConnection) apiURL.openConnection();

      conn.setRequestMethod("POST");
      conn.setDoOutput(true);
      conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
      wr.write(param.toString());
      wr.flush();

      BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();

      while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
      }
      in.close();
      //			System.out.println(response.toString());
      return response.toString();

    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }
Exemplo n.º 2
0
  /**
   * Retrieves a list of parameters from a passed request in comma delimited format. If the
   * parameter is not found, an NPE is thrown.
   *
   * @param req Request object to look in
   * @param name the name of the field from the html form
   * @return the value from the html form corresponding to the name parameter
   * @exception NullPointerException thrown if the name was not in the html form
   */
  public static String getOptionalParameterValues(ServletRequest req, String name)
      throws NullPointerException {
    String[] values = req.getParameterValues(name);

    if (values == null) {
      return Config.EMPTY_STR;
    }

    StringBuffer ret = new StringBuffer();

    for (int i = 0; i < values.length; i++) {
      if (i > 0) {
        ret.append(",");
      }
      ret.append(values[i]);
    }
    return ret.toString();
  }
Exemplo n.º 3
0
  /**
   * Retrieves a list of parameters from a passed request in comma delimited format. If the
   * parameter is not found, an NPE is thrown.
   *
   * @param req Request object to look in
   * @param name the name of the field from the html form
   * @return the value from the html form corresponding to the name parameter
   * @exception NullPointerException thrown if the name was not in the html form
   */
  public static String getRequiredParameterValues(ServletRequest req, String name)
      throws NullPointerException {
    String[] values = req.getParameterValues(name);

    if (values == null) {
      throw new NullPointerException(
          "This form requires a \""
              + name
              + "\" parameter, which was missing from the submitted request.");
    }

    StringBuffer ret = new StringBuffer();

    for (int i = 0; i < values.length; i++) {
      if (i > 0) {
        ret.append(",");
      }
      ret.append(values[i]);
    }
    return ret.toString();
  }
  public void doProcess(HttpServletRequest req, HttpServletResponse res, boolean isPost) {
    StringBuffer bodyContent = null;
    OutputStream out = null;
    PrintWriter writer = null;
    String serviceKey = null;

    try {
      BufferedReader in = req.getReader();
      String line = null;
      while ((line = in.readLine()) != null) {
        if (bodyContent == null) bodyContent = new StringBuffer();
        bodyContent.append(line);
      }
    } catch (Exception e) {
    }
    try {
      if (requireSession) {
        // check to see if there was a session created for this request
        // if not assume it was from another domain and blow up
        // Wrap this to prevent Portlet exeptions
        HttpSession session = req.getSession(false);
        if (session == null) {
          res.setStatus(HttpServletResponse.SC_FORBIDDEN);
          return;
        }
      }
      serviceKey = req.getParameter("id");
      // only to preven regressions - Remove before 1.0
      if (serviceKey == null) serviceKey = req.getParameter("key");
      // check if the services have been loaded or if they need to be reloaded
      if (services == null || configUpdated()) {
        getServices(res);
      }
      String urlString = null;
      String xslURLString = null;
      String userName = null;
      String password = null;
      String format = "json";
      String callback = req.getParameter("callback");
      String urlParams = req.getParameter("urlparams");
      String countString = req.getParameter("count");
      // encode the url to prevent spaces from being passed along
      if (urlParams != null) {
        urlParams = urlParams.replace(' ', '+');
      }

      try {
        if (services.has(serviceKey)) {
          JSONObject service = services.getJSONObject(serviceKey);
          // default to the service default if no url parameters are specified
          if (urlParams == null && service.has("defaultURLParams")) {
            urlParams = service.getString("defaultURLParams");
          }
          String serviceURL = service.getString("url");
          // build the URL
          if (urlParams != null && serviceURL.indexOf("?") == -1) {
            serviceURL += "?";
          } else if (urlParams != null) {
            serviceURL += "&";
          }
          String apikey = "";
          if (service.has("username")) userName = service.getString("username");
          if (service.has("password")) password = service.getString("password");
          if (service.has("apikey")) apikey = service.getString("apikey");
          urlString = serviceURL + apikey;
          if (urlParams != null) urlString += "&" + urlParams;
          if (service.has("xslStyleSheet")) {
            xslURLString = service.getString("xslStyleSheet");
          }
        }
        // code for passing the url directly through instead of using configuration file
        else if (req.getParameter("url") != null) {
          String serviceURL = req.getParameter("url");
          // build the URL
          if (urlParams != null && serviceURL.indexOf("?") == -1) {
            serviceURL += "?";
          } else if (urlParams != null) {
            serviceURL += "&";
          }
          urlString = serviceURL;
          if (urlParams != null) urlString += urlParams;
        } else {
          writer = res.getWriter();
          if (serviceKey == null)
            writer.write("XmlHttpProxyServlet Error: id parameter specifying serivce required.");
          else
            writer.write(
                "XmlHttpProxyServlet Error : service for id '" + serviceKey + "' not  found.");
          writer.flush();
          return;
        }
      } catch (Exception ex) {
        getLogger().severe("XmlHttpProxyServlet Error loading service: " + ex);
      }

      Map paramsMap = new HashMap();
      paramsMap.put("format", format);
      // do not allow for xdomain unless the context level setting is enabled.
      if (callback != null && allowXDomain) {
        paramsMap.put("callback", callback);
      }
      if (countString != null) {
        paramsMap.put("count", countString);
      }

      InputStream xslInputStream = null;

      if (urlString == null) {
        writer = res.getWriter();
        writer.write(
            "XmlHttpProxyServlet parameters:  id[Required] urlparams[Optional] format[Optional] callback[Optional]");
        writer.flush();
        return;
      }
      // default to JSON
      res.setContentType(responseContentType);
      out = res.getOutputStream();
      // get the stream for the xsl stylesheet
      if (xslURLString != null) {
        // check the web root for the resource
        URL xslURL = null;
        xslURL = ctx.getResource(resourcesDir + "xsl/" + xslURLString);
        // if not in the web root check the classpath
        if (xslURL == null) {
          xslURL =
              XmlHttpProxyServlet.class.getResource(classpathResourcesDir + "xsl/" + xslURLString);
        }
        if (xslURL != null) {
          xslInputStream = xslURL.openStream();
        } else {
          String message =
              "Could not locate the XSL stylesheet provided for service id "
                  + serviceKey
                  + ". Please check the XMLHttpProxy configuration.";
          getLogger().severe(message);
          try {
            out.write(message.getBytes());
            out.flush();
            return;
          } catch (java.io.IOException iox) {
          }
        }
      }
      if (!isPost) {
        xhp.doGet(urlString, out, xslInputStream, paramsMap, userName, password);
      } else {
        if (bodyContent == null)
          getLogger()
              .info(
                  "XmlHttpProxyServlet attempting to post to url "
                      + urlString
                      + " with no body content");
        xhp.doPost(
            urlString,
            out,
            xslInputStream,
            paramsMap,
            bodyContent.toString(),
            req.getContentType(),
            userName,
            password);
      }
    } catch (Exception iox) {
      iox.printStackTrace();
      getLogger().severe("XmlHttpProxyServlet: caught " + iox);
      try {
        writer = res.getWriter();
        writer.write(iox.toString());
        writer.flush();
      } catch (java.io.IOException ix) {
        ix.printStackTrace();
      }
      return;
    } finally {
      try {
        if (out != null) out.close();
        if (writer != null) writer.close();
      } catch (java.io.IOException iox) {
      }
    }
  }
  /**
   * Sends an HTTP GET request to a url
   *
   * @param url the url
   * @return - HTTP response code
   */
  public int sendGetRequest(String url)
      throws IOException, OAuthMessageSignerException, OAuthExpectationFailedException,
          OAuthCommunicationException {

    System.out.println("url in sendrequest= " + url);
    int responseCode = 500;
    try {
      HttpURLConnection uc = getConnection(url);

      responseCode = uc.getResponseCode();

      if (200 == responseCode || 401 == responseCode || 404 == responseCode) {
        BufferedReader rd =
            new BufferedReader(
                new InputStreamReader(
                    responseCode == 200 ? uc.getInputStream() : uc.getErrorStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = rd.readLine()) != null) {
          System.out.println("line = " + line);
          sb.append(line);
          System.out.println("sb.append = " + sb);
        }
        String response = sb.toString();
        try {
          JSONObject json = new JSONObject(response);

          System.out.println("\nResults:");
          System.out.println(
              "Total results = "
                  + json.getJSONObject("bossresponse")
                      .getJSONObject("web")
                      .getString("totalresults"));

          System.out.println();

          JSONArray ja =
              json.getJSONObject("bossresponse").getJSONObject("web").getJSONArray("results");
          BufferedWriter out = new BufferedWriter(new FileWriter("outfile.csv", true));
          String str = "";
          System.out.println("\nResults:");
          for (int i = 0; i < ja.length(); i++) {
            // System.out.print((i+1) + ". ");
            JSONObject j = ja.getJSONObject(i);
            str += j.getString("url") + "," + j.getString("abstract") + "\n";
            out.write(str);
          }

        } catch (Exception e) {
          System.err.println("Something went wrong...");
          e.printStackTrace();
        }
        rd.close();
        setResponseBody(sb.toString());
      }
    } catch (MalformedURLException ex) {
      throw new IOException(url + " is not valid");
    } catch (IOException ie) {
      throw new IOException("IO Exception " + ie.getMessage());
    }
    return responseCode;
  }
Exemplo n.º 6
0
    protected String[] doInBackground(String... params) {
      HttpURLConnection urlConnection = null;
      // These two need to be declared outside the try/catch
      // so that they can be closed in the finally block
      BufferedReader reader = null;

      // Will contain the raw JSON response as a string.
      String forecastJsonStr = null;

      String location = "";
      if (params.length > 0) location = params[0];
      // Log.v(LOG_TAG, "Location value: " + location);
      String format = "json";
      String units = "metric";
      int numDays = 7;
      String apiKey = "bd82977b86bf27fb59a04b61b657fb6f";

      try {
        // Construct the URL for the OpenWeatherMap query
        // Possible parameters are avaiable at OWM's forecast API page, at
        // http://openweathermap.org/API#forecast
        final String FORECAST_BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?";
        final String QUERY_PARAM = "zip";
        final String FORMAT_PARAM = "mode";
        final String UNIT_PARAM = "units";
        final String DAYS_PARAM = "cnt";
        final String KEY_PARAM = "appid";

        Uri builtUri =
            Uri.parse(FORECAST_BASE_URL)
                .buildUpon()
                .appendQueryParameter(QUERY_PARAM, location + ",us")
                .appendQueryParameter(FORMAT_PARAM, format)
                .appendQueryParameter(UNIT_PARAM, units)
                .appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
                .appendQueryParameter(KEY_PARAM, apiKey)
                .build();
        URL url = new URL(builtUri.toString());

        // Log.v(LOG_TAG,"Built URI: " + builtUri.toString());
        // URL url = new
        // URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=NewYork,us&mode=json&units=metric&cnt=7&appid=bd82977b86bf27fb59a04b61b657fb6f");

        // Create the request to OpenWeatherMap, and open the connection
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

        // Read the input stream into a String
        InputStream inputStream = urlConnection.getInputStream();
        StringBuffer buffer = new StringBuffer();
        if (inputStream == null) {
          // Nothing to do.
          return null;
        }
        reader = new BufferedReader(new InputStreamReader(inputStream));

        String line;
        while ((line = reader.readLine()) != null) {
          // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
          // But it does make debugging a *lot* easier if you print out the completed
          // buffer for debugging.
          buffer.append(line + "\n");
        }

        if (buffer.length() == 0) {
          // Stream was empty.  No point in parsing.
          return null;
        }
        forecastJsonStr = buffer.toString();

      } catch (IOException e) {
        Log.e(LOG_TAG, "Error ", e);
        // If the code didn't successfully get the weather data, there's no point in attemping
        // to parse it.
        return null;
      } finally {
        if (urlConnection != null) {
          urlConnection.disconnect();
        }
        if (reader != null) {
          try {
            reader.close();
          } catch (final IOException e) {
            Log.e(LOG_TAG, "Error closing stream", e);
          }
        }
      }
      try {
        return getWeatherDataFromJson(forecastJsonStr, numDays);
      } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        e.printStackTrace();
      }
      return null;
    }