Пример #1
0
 public String get(String path, Map<String, String> params) {
   try {
     GetMethod getMethod = new GetMethod(baseUrl() + path);
     if (params != null) {
       getMethod.setParams(httpParams(params));
     }
     int returnCode = client().executeMethod(getMethod);
     if (returnCode >= 200 && returnCode <= 200) {
       return getMethod.getResponseBodyAsString();
     }
     throw new RuntimeException(
         String.format(
             "The request to [%s] could not be completed. Response [%s] was returned with code [%s]",
             path, getMethod.getResponseBodyAsString(), returnCode));
   } catch (IOException e) {
     throw new RuntimeException("Connection pooped", e);
   }
 }
Пример #2
0
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    GetMethod httpGet = null;

    try {
      String url = RequestUtil.getParameter(request, PARAM_URL, defaultProxyUrl);
      String host = url.split("/")[2];

      // Get the proxy parameters
      // TODO: Add dependency injection to set proxy config from GeoNetwork settings, using also the
      // credentials configured
      String proxyHost = System.getProperty("http.proxyHost");
      String proxyPort = System.getProperty("http.proxyPort");

      // Get rest of parameters to pass to proxied url
      HttpMethodParams urlParams = new HttpMethodParams();

      Enumeration paramNames = request.getParameterNames();
      while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if (!paramName.equalsIgnoreCase(PARAM_URL)) {
          urlParams.setParameter(paramName, request.getParameter(paramName));
        }
      }

      // Checks if allowed host
      if (!isAllowedHost(host)) {
        // throw new ServletException("This proxy does not allow you to access that location.");
        returnExceptionMessage(response, "This proxy does not allow you to access that location.");
        return;
      }

      if (url.startsWith("http://") || url.startsWith("https://")) {
        HttpClient client = new HttpClient();

        // Added support for proxy
        if (proxyHost != null && proxyPort != null) {
          client.getHostConfiguration().setProxy(proxyHost, Integer.valueOf(proxyPort));
        }

        httpGet = new GetMethod(url);
        httpGet.setParams(urlParams);
        client.executeMethod(httpGet);

        if (httpGet.getStatusCode() == HttpStatus.SC_OK) {
          Header contentType = httpGet.getResponseHeader(HEADER_CONTENT_TYPE);
          String[] contentTypesReturned = contentType.getValue().split(";");
          if (!isValidContentType(contentTypesReturned[0])) {
            contentTypesReturned = contentType.getValue().split(" ");
            if (!isValidContentType(contentTypesReturned[0])) {
              throw new ServletException("Status: 415 Unsupported media type");
            }
          }

          // Sets response contentType
          response.setContentType(getResponseContentType(contentTypesReturned));

          String responseBody = httpGet.getResponseBodyAsString().trim();

          PrintWriter out = response.getWriter();
          out.print(responseBody);

          out.flush();
          out.close();

        } else {
          returnExceptionMessage(
              response, "Unexpected failure: " + httpGet.getStatusLine().toString());
        }

        httpGet.releaseConnection();

      } else {
        // throw new ServletException("only HTTP(S) protocol supported");
        returnExceptionMessage(response, "only HTTP(S) protocol supported");
      }
    } catch (Exception e) {
      e.printStackTrace();
      // throw new ServletException("Some unexpected error occurred. Error text was: " +
      // e.getMessage());
      returnExceptionMessage(
          response, "Some unexpected error occurred. Error text was: " + e.getMessage());
    } finally {
      if (httpGet != null) httpGet.releaseConnection();
    }
  }