Package net.sf.sahi.response

Examples of net.sf.sahi.response.HttpResponse


import net.sf.sahi.response.HttpResponse;

public class Substituter {

    public HttpResponse replaceHttps(final HttpRequest request) {
        HttpResponse response = new RemoteRequestProcessor().processHttp(request);
        byte[] bytes = response.data();
        String data = new String(bytes);
        data = data.replaceAll("https", "http");
        response.setData(data.getBytes());
        return response;
    }
View Full Code Here


import net.sf.sahi.response.SimpleHttpResponse;
import net.sf.sahi.util.URLParser;

public class LocalRequestProcessor {
    public HttpResponse getLocalResponse(String uri, HttpRequest requestFromBrowser) throws Exception {
        HttpResponse httpResponse = new NoCacheHttpResponse("");
        if (uri.indexOf("/dyn/pro/") != -1) {
//          httpResponse = handleDyn(uri, requestFromBrowser, httpResponse, true);
          httpResponse = new SimpleHttpResponse("This feature is available only in Sahi Pro");
        } else if (uri.indexOf("/dyn/") != -1) {
            httpResponse = handleDyn(uri, requestFromBrowser, httpResponse, false);
View Full Code Here

            processConnect(requestFromBrowser);
        } else {
            if (handleDifferently(requestFromBrowser)) {
                return;
            }
            HttpResponse responseFromHost = null;
            try {
                responseFromHost = remoteRequestProcessor.processHttp(requestFromBrowser);
            } catch (Exception e) {
                e.printStackTrace();
                responseFromHost = new SimpleHttpResponse("");
View Full Code Here

        }
    }

    private boolean handleDifferently(final HttpRequest request) throws Exception {
        final MockResponder mockResponder = request.session().mockResponder();
        HttpResponse response = mockResponder.getResponse(request);
        if (response == null) {
            return false;
        }
        sendResponseToBrowser(response);
        return true;
View Full Code Here

        return false;
  }


    private void processLocally(String uri, final HttpRequest requestFromBrowser) throws IOException {
      HttpResponse httpResponse;
      try {
        httpResponse = new LocalRequestProcessor().getLocalResponse(uri, requestFromBrowser);
      } catch (Exception e) {
        Properties props = new Properties();
        props.put("responseCode", "500");
View Full Code Here

    }

    public HttpResponse simple(final HttpRequest request) throws IOException {
        Properties props = new Properties();
        props.put("url", request.url());
        HttpResponse mockResponse = new HttpFileResponse(Configuration.getHtdocsRoot() + "spr/simpleMock.htm", props, false, false);
        HttpResponse response = new HttpModifiedResponse2(mockResponse, request.isSSL(), request.fileExtension());
//    response.addFilter(new ChunkedFilter());
    return response;
    }
View Full Code Here

      if (logger.isLoggable(Level.FINEST)){
        logger.finest("Request headers set on connection:");
        logger.finest(getReqHeaders(connection));
      }     

      HttpResponse response = null;
      int responseCode = -1;
      try{
        connection.setRequestMethod(requestFromBrowser.method().toUpperCase());
        if (requestFromBrowser.isPost() || requestFromBrowser.isPut()){
          if (logger.isLoggable(Level.FINEST)){
            logger.finest("In post requestFromBrowser.data() = " + requestFromBrowser.data());
          }
          connection.setDoOutput(true);
          OutputStream outputStreamToHost = connection.getOutputStream();
          outputStreamToHost.write(requestFromBrowser.data());
          outputStreamToHost.close();
        }
        InputStream inputStreamFromHost = null;
        responseCode = connection.getResponseCode();
        if (logger.isLoggable(Level.FINE)){
          logger.fine("responseCode  = " + responseCode);
        }
        if (responseCode < 400){
          inputStreamFromHost = connection.getInputStream();
            }else{
              if (logger.isLoggable(Level.FINE)){
            logger.fine("Fetching error stream");
          }
              inputStreamFromHost = connection.getErrorStream();
            }
        boolean isGZIP = "gzip".equals(connection.getContentEncoding());
        if (logger.isLoggable(Level.FINER)){
          logger.finer("isGZIP=" + isGZIP + "; connection.getContentEncoding()=" + connection.getContentEncoding());
        }       
        if (isGZIP){
          if (logger.isLoggable(Level.FINE)){
            logger.fine("Using GZIPInputStream");
          }
          try {
            inputStreamFromHost = new GZIPInputStream(inputStreamFromHost);
          } catch (IOException ioe) {
            // happens for redirects etc. where there is no body. Ignore
          }
        }
        if (responseCode >= 500 && !requestFromBrowser.isAjax()){
          if (logger.isLoggable(Level.FINE)){
            logger.fine("Returning get5xxResponse");
          }
          response = getWrappedResponse(get5xxResponse(responseCode, inputStreamFromHost));
        } else if (responseCode == 401){
          if (logger.isLoggable(Level.FINE)){
            logger.fine("Returning process401");
          }
          response = process401(connection, inputStreamFromHost);
        }else {
          response = getResponse(inputStreamFromHost, connection);
          TrafficLogger.storeResponseHeader(response.headers().toString().getBytes(), "unmodified");
          TrafficLogger.storeResponseBody(response.data(), "unmodified");
        }
       
        if (requestFromBrowser.isAjax() && responseCode > 300 && responseCode < 308){
          String redirectedTo = response.getLastSetValueOfHeader("Location");
          if (redirectedTo != null) session.addAjaxRedirect(redirectedTo);
        }
       
        if (isGZIP) {
          response.removeHeader("Content-Encoding", "gzip");
          if (response instanceof StreamingHttpResponse) response.setContentLength(-1);
        }
       
      }catch(IOException uhe){
        if (uhe instanceof SSLHandshakeException)
          uhe.printStackTrace();
        if (logger.isLoggable(Level.WARNING) && !urlStr.contains("DesignerOutput")){
          logger.warning("Returning CannotConnectResponse for: " + urlStr);
          logger.fine(Utils.getStackTraceString(uhe));
        }
        response = getWrappedResponse(getCannotConnectResponse(uhe));
      }
      if (responseCode != 204 && responseCode != 304){
        String contentTypeHeader = response.contentTypeHeader();
        boolean downloadContentType = isDownloadContentType(contentTypeHeader);
        boolean attachment = response.isAttachment();

        String fileName = null;
        boolean contentDispositionForcesDownload = false;
     
        if (fileName == nullfileName = requestFromBrowser.fileName();
       
        boolean downloadURL = isDownloadURL(urlStr);
        if (logger.isLoggable(Level.FINER)){
          logger.finer("downloadURL = " + downloadURL);
          logger.finer("response.isAttachment() = " + attachment);
          logger.finer("fileName = " + fileName);
          logger.finer("contentTypeHeader = " + contentTypeHeader);
          logger.finer("downloadContentType = " + downloadContentType);
          logger.finer("Content-Disposition=" + response.getLastSetValueOfHeader("Content-Disposition"));
        }
        if (responseCode == 200
            && (downloadContentType ||
                contentDispositionForcesDownload ||
                downloadURL)) {
          if (logger.isLoggable(Level.INFO)){
            StringBuilder sb = new StringBuilder();
            sb.append("\n-- Calling downloadFile --\n");
            sb.append(requestFromBrowser.url());
            sb.append("\ndownloadURL = " + downloadURL);
            sb.append("\nresponse.isAttachment() = " + attachment);
            sb.append("\nfileName = " + fileName);
            sb.append("\ncontentTypeHeader = " + contentTypeHeader);
            sb.append("\ndownloadContentType = " + downloadContentType);
            sb.append("\nContent-Disposition=" + response.getLastSetValueOfHeader("Content-Disposition"));
            sb.append("\ncontentDispositionForcesDownload = " + contentDispositionForcesDownload);
            sb.append("\n--");
            logger.info(sb.toString());
          }
          downloadFile(requestFromBrowser, response, fileName);
View Full Code Here

    final String contentType = connection.getContentType();
    if (useStreaming || (contentType != null && (contentType.contains("video")))){
      logger.info("Using streaming response for contentType: " + contentType);
      return new StreamingHttpResponse(inputStreamFromHost, connection);
    }
    return new HttpResponse(inputStreamFromHost, connection);
  }
View Full Code Here

    return response;
  }

  private HttpResponse process401(HttpURLConnection connection, InputStream inputStreamFromHost)
      throws IOException {
    HttpResponse response;
    response = getResponse(inputStreamFromHost, connection);
    String wwwAuthenticate = response.getLastSetValueOfHeader("WWW-Authenticate");
    List<String> cookieHeaders = response.headers().getHeaders("Set-Cookie");
    if (logger.isLoggable(Level.INFO)){
      logger.info("wwwAuthenticate: " + wwwAuthenticate);
    }
    if (wwwAuthenticate != null){
      String scheme = getScheme(wwwAuthenticate);
      String realm = getRealm(wwwAuthenticate);
      if (logger.isLoggable(Level.INFO)){
        logger.info("scheme=" + scheme + "; realm=" + realm);
      }
      Properties props = new Properties();
      props.put("realm", ""+realm);
      props.put("scheme", ""+scheme);
      props.put("authKey", "\"" + Utils.makeString((realm != null) ? realm : scheme) + "\"");
     
      if (!"ntlm".equals(scheme)) {
        String message = "";
        try{
          message = new String(Utils.getBytes(inputStreamFromHost));
        } catch(Exception e) {
          logger.info(Utils.getStackTraceString(e));
        }
        props.put("message", message);
      }
      response = getWrappedResponse(new HttpFileResponse(Configuration.getHtdocsRoot() + "/spr/401.htm", props, false, false));
      response.headers().addHeaders("Set-Cookie", cookieHeaders);
    }
    return response;
  }
View Full Code Here

    return scriptRunner.getScript();
  }

    public HttpResponse currentBrowserScript(final HttpRequest request) {
        Session session = request.session();
        HttpResponse httpResponse;
        if (session.getScriptRunner() != null && getScript(session) != null) {
            httpResponse = new SimpleHttpResponse(LogViewer.highlight(getScript(session).getBrowserJS(), -1));
        } else {
            httpResponse = new SimpleHttpResponse(
                    "No Script has been set for playback.");
View Full Code Here

TOP

Related Classes of net.sf.sahi.response.HttpResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.