示例#1
0
  /**
   * Constructs a service url from the HttpServletRequest or from the given serviceUrl. Prefers the
   * serviceUrl provided if both a serviceUrl and a serviceName.
   *
   * @param request the HttpServletRequest
   * @param response the HttpServletResponse
   * @param service the configured service url (this will be used if not null)
   * @param serverName the server name to use to constuct the service url if the service param is
   *     empty
   * @param artifactParameterName the artifact parameter name to remove (i.e. ticket)
   * @param encode whether to encode the url or not (i.e. Jsession).
   * @return the service url to use.
   */
  public static String constructServiceUrl(
      final HttpServletRequest request,
      final HttpServletResponse response,
      final String service,
      final String serverName,
      final String artifactParameterName,
      final boolean encode) {
    if (CommonUtils.isNotBlank(service)) {
      return encode ? response.encodeURL(service) : service;
    }

    final StringBuilder buffer = new StringBuilder();

    if (!serverName.startsWith("https://") && !serverName.startsWith("http://")) {
      buffer.append(request.isSecure() ? "https://" : "http://");
    }

    buffer.append(serverName);
    buffer.append(request.getRequestURI());

    if (CommonUtils.isNotBlank(request.getQueryString())) {
      final int location = request.getQueryString().indexOf(artifactParameterName + "=");

      if (location == 0) {
        final String returnValue =
            encode ? response.encodeURL(buffer.toString()) : buffer.toString();
        if (LOG.isDebugEnabled()) {
          LOG.debug("serviceUrl generated: " + returnValue);
        }
        return returnValue;
      }

      buffer.append("?");

      if (location == -1) {
        buffer.append(request.getQueryString());
      } else if (location > 0) {
        final int actualLocation =
            request.getQueryString().indexOf("&" + artifactParameterName + "=");

        if (actualLocation == -1) {
          buffer.append(request.getQueryString());
        } else if (actualLocation > 0) {
          buffer.append(request.getQueryString().substring(0, actualLocation));
        }
      }
    }

    final String returnValue = encode ? response.encodeURL(buffer.toString()) : buffer.toString();
    if (LOG.isDebugEnabled()) {
      LOG.debug("serviceUrl generated: " + returnValue);
    }
    return returnValue;
  }
示例#2
0
  /**
   * Contacts the remote URL and returns the response.
   *
   * @param constructedUrl the url to contact.
   * @param hostnameVerifier Host name verifier to use for HTTPS connections.
   * @param encoding the encoding to use.
   * @return the response.
   */
  public static String getResponseFromServer(
      final URL constructedUrl, final HostnameVerifier hostnameVerifier, final String encoding) {
    URLConnection conn = null;
    try {
      conn = constructedUrl.openConnection();
      if (conn instanceof HttpsURLConnection) {
        ((HttpsURLConnection) conn).setHostnameVerifier(hostnameVerifier);
      }
      final BufferedReader in;

      if (CommonUtils.isEmpty(encoding)) {
        in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      } else {
        in = new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
      }

      String line;
      final StringBuilder stringBuffer = new StringBuilder(255);

      while ((line = in.readLine()) != null) {
        stringBuffer.append(line);
        stringBuffer.append("\n");
      }
      return stringBuffer.toString();
    } catch (final Exception e) {
      LOG.error(e.getMessage(), e);
      throw new RuntimeException(e);
    } finally {
      if (conn != null && conn instanceof HttpURLConnection) {
        ((HttpURLConnection) conn).disconnect();
      }
    }
  }