Example usage for java.net URI toURL

List of usage examples for java.net URI toURL

Introduction

In this page you can find the example usage for java.net URI toURL.

Prototype

public URL toURL() throws MalformedURLException 

Source Link

Document

Constructs a URL from this URI.

Usage

From source file:microsoft.exchange.webservices.data.core.ExchangeServiceBase.java

private void prepareHttpWebRequestForUrl(URI url, boolean acceptGzipEncoding, boolean allowAutoRedirect,
        HttpClientWebRequest request) throws ServiceLocalException, URISyntaxException {
    try {/*from   w  ww .  ja v a2  s .c o  m*/
        request.setUrl(url.toURL());
    } catch (MalformedURLException e) {
        String strErr = String.format("Incorrect format : %s", url);
        throw new ServiceLocalException(strErr);
    }

    request.setPreAuthenticate(preAuthenticate);
    request.setTimeout(timeout);
    request.setContentType("text/xml; charset=utf-8");
    request.setAccept("text/xml");
    request.setUserAgent(userAgent);
    request.setAllowAutoRedirect(allowAutoRedirect);
    request.setAcceptGzipEncoding(acceptGzipEncoding);
    request.setHeaders(getHttpHeaders());
    request.setProxy(getWebProxy());
    prepareCredentials(request);

    request.prepareConnection();

    httpResponseHeaders.clear();
}

From source file:org.apache.beam.runners.apex.ApexRunner.java

@Override
public ApexRunnerResult run(final Pipeline pipeline) {
    pipeline.replaceAll(getOverrides());

    final ApexPipelineTranslator translator = new ApexPipelineTranslator(options);
    final AtomicReference<DAG> apexDAG = new AtomicReference<>();
    final AtomicReference<File> tempDir = new AtomicReference<>();

    StreamingApplication apexApp = (dag, conf) -> {
        apexDAG.set(dag);/*w  w  w .  j  a va2 s .  co  m*/
        dag.setAttribute(DAGContext.APPLICATION_NAME, options.getApplicationName());
        if (options.isEmbeddedExecution()) {
            // set unique path for application state to allow for parallel execution of unit tests
            // (the embedded cluster would set it to a fixed location under ./target)
            tempDir.set(Files.createTempDir());
            dag.setAttribute(DAGContext.APPLICATION_PATH, tempDir.get().toURI().toString());
        }
        translator.translate(pipeline, dag);
    };

    Properties configProperties = new Properties();
    try {
        if (options.getConfigFile() != null) {
            URI configURL = new URI(options.getConfigFile());
            if (CLASSPATH_SCHEME.equals(configURL.getScheme())) {
                InputStream is = this.getClass().getResourceAsStream(configURL.getPath());
                if (is != null) {
                    configProperties.load(is);
                    is.close();
                }
            } else {
                if (!configURL.isAbsolute()) {
                    // resolve as local file name
                    File f = new File(options.getConfigFile());
                    configURL = f.toURI();
                }
                try (InputStream is = configURL.toURL().openStream()) {
                    configProperties.load(is);
                }
            }
        }
    } catch (IOException | URISyntaxException ex) {
        throw new RuntimeException("Error loading properties", ex);
    }

    if (options.isEmbeddedExecution()) {
        EmbeddedAppLauncher<?> launcher = Launcher.getLauncher(LaunchMode.EMBEDDED);
        Attribute.AttributeMap launchAttributes = new Attribute.AttributeMap.DefaultAttributeMap();
        launchAttributes.put(EmbeddedAppLauncher.RUN_ASYNC, true);
        if (options.isEmbeddedExecutionDebugMode()) {
            // turns off timeout checking for operator progress
            launchAttributes.put(EmbeddedAppLauncher.HEARTBEAT_MONITORING, false);
        }
        Configuration conf = new Configuration(false);
        ApexYarnLauncher.addProperties(conf, configProperties);
        try {
            if (translateOnly) {
                launcher.prepareDAG(apexApp, conf);
                return new ApexRunnerResult(launcher.getDAG(), null);
            }
            ApexRunner.ASSERTION_ERROR.set(null);
            AppHandle apexAppResult = launcher.launchApp(apexApp, conf, launchAttributes);
            return new ApexRunnerResult(apexDAG.get(), apexAppResult) {
                @Override
                protected void cleanupOnCancelOrFinish() {
                    if (tempDir.get() != null) {
                        FileUtils.deleteQuietly(tempDir.get());
                    }
                }
            };
        } catch (Exception e) {
            Throwables.throwIfUnchecked(e);
            throw new RuntimeException(e);
        }
    } else {
        try {
            ApexYarnLauncher yarnLauncher = new ApexYarnLauncher();
            AppHandle apexAppResult = yarnLauncher.launchApp(apexApp, configProperties);
            return new ApexRunnerResult(apexDAG.get(), apexAppResult);
        } catch (IOException e) {
            throw new RuntimeException("Failed to launch the application on YARN.", e);
        }
    }
}

From source file:it.staiger.jmeter.protocol.http.sampler.HTTPHC4DynamicFilePost.java

/**
 * Parses the result and fills the SampleResult with its info
 * @param httpRequest the executed request
 * @param localContext the Http context which was used
 * @param res the SampleResult which is to be filled
 * @param httpResponse the response which is to be parsed
 * @throws IllegalStateException//from   w ww .  j a  v a2s .c o  m
 * @throws IOException
 */
private void parseResponse(HttpRequestBase httpRequest, HttpContext localContext, HTTPSampleResult res,
        HttpResponse httpResponse) throws IllegalStateException, IOException {

    // Needs to be done after execute to pick up all the headers
    final HttpRequest request = (HttpRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
    // We've finished with the request, so we can add the LocalAddress to it for display
    final InetAddress localAddr = (InetAddress) httpRequest.getParams()
            .getParameter(ConnRoutePNames.LOCAL_ADDRESS);
    if (localAddr != null) {
        request.addHeader(HEADER_LOCAL_ADDRESS, localAddr.toString());
    }
    res.setRequestHeaders(getConnectionHeaders(request));

    Header contentType = httpResponse.getLastHeader(HTTPConstants.HEADER_CONTENT_TYPE);
    if (contentType != null) {
        String ct = contentType.getValue();
        res.setContentType(ct);
        res.setEncodingAndType(ct);
    }
    HttpEntity entity = httpResponse.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        res.setResponseData(readResponse(res, instream, (int) entity.getContentLength()));
    }

    res.sampleEnd(); // Done with the sampling proper.
    currentRequest = null;

    // Now collect the results into the HTTPSampleResult:
    StatusLine statusLine = httpResponse.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    res.setResponseCode(Integer.toString(statusCode));
    res.setResponseMessage(statusLine.getReasonPhrase());
    res.setSuccessful(isSuccessCode(statusCode));

    res.setResponseHeaders(getResponseHeaders(httpResponse));
    if (res.isRedirect()) {
        final Header headerLocation = httpResponse.getLastHeader(HTTPConstants.HEADER_LOCATION);
        if (headerLocation == null) { // HTTP protocol violation, but avoids NPE
            throw new IllegalArgumentException(
                    "Missing location header in redirect for " + httpRequest.getRequestLine());
        }
        String redirectLocation = headerLocation.getValue();
        res.setRedirectLocation(redirectLocation);
    }

    // record some sizes to allow HTTPSampleResult.getBytes() with different options
    HttpConnectionMetrics metrics = (HttpConnectionMetrics) localContext.getAttribute(CONTEXT_METRICS);
    long headerBytes = res.getResponseHeaders().length() // condensed length (without \r)
            + httpResponse.getAllHeaders().length // Add \r for each header
            + 1 // Add \r for initial header
            + 2; // final \r\n before data
    long totalBytes = metrics.getReceivedBytesCount();
    res.setHeadersSize((int) headerBytes);
    res.setBodySize((int) (totalBytes - headerBytes));
    if (log.isDebugEnabled()) {
        log.debug("ResponseHeadersSize=" + res.getHeadersSize() + " Content-Length=" + res.getBodySize()
                + " Total=" + (res.getHeadersSize() + res.getBodySize()));
    }

    // If we redirected automatically, the URL may have changed
    if (getAutoRedirects()) {
        HttpUriRequest req = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
        HttpHost target = (HttpHost) localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        URI redirectURI = req.getURI();
        if (redirectURI.isAbsolute()) {
            res.setURL(redirectURI.toURL());
        } else {
            res.setURL(new URL(new URL(target.toURI()), redirectURI.toString()));
        }
    }
}

From source file:org.apache.airavata.workflow.model.wf.Workflow.java

/**
 * Constructs a workflow from a given URI.
 * @param workflowFilePath The workflow URI path.
 * @throws GraphException If an error occurred while creating workflow.
 * @throws ComponentException If an error occurred while parsing the workflow content.
 *//*  w ww  .j  a va2  s. c o m*/
public Workflow(URI workflowFilePath) throws GraphException, ComponentException {
    this();
    try {
        XmlElement workflowElement = XMLUtil.loadXML(workflowFilePath.toURL().openStream());
        parse(workflowElement);
    } catch (RuntimeException e) {
        throw new GraphException(e);
    } catch (IOException e) {
        throw new GraphException(e);
    }
}

From source file:org.geoserver.wms.web.data.ExternalGraphicPanel.java

/**
 * @param id/*  ww  w  . ja  va 2s . c  o m*/
 * @param model Must return a {@link ResourceInfo}
 */
public ExternalGraphicPanel(String id, final CompoundPropertyModel<StyleInfo> styleModel,
        final Form<?> styleForm) {
    super(id, styleModel);

    // container for ajax updates
    final WebMarkupContainer container = new WebMarkupContainer("externalGraphicContainer");
    container.setOutputMarkupId(true);
    add(container);

    table = new WebMarkupContainer("list");
    table.setOutputMarkupId(true);

    IModel<String> bind = styleModel.bind("legend.onlineResource");
    onlineResource = new TextField<String>("onlineResource", bind);
    onlineResource.add(new IValidator<String>() {
        final List<String> EXTENSIONS = Arrays.asList(new String[] { "png", "gif", "jpeg", "jpg" });

        @Override
        public void validate(IValidatable<String> input) {
            String value = input.getValue();
            int last = value == null ? -1 : value.lastIndexOf('.');
            if (last == -1 || !EXTENSIONS.contains(value.substring(last + 1).toLowerCase())) {
                ValidationError error = new ValidationError();
                error.setMessage("Not an image");
                error.addKey("nonImage");
                input.error(error);
                return;
            }
            URI uri = null;
            try {
                uri = new URI(value);
            } catch (URISyntaxException e1) {
                // Unable to check if absolute
            }
            if (uri != null && uri.isAbsolute()) {
                try {
                    String baseUrl = baseURL(onlineResource.getForm());
                    if (!value.startsWith(baseUrl)) {
                        onlineResource.warn("Recommend use of styles directory at " + baseUrl);
                    }
                    URL url = uri.toURL();
                    URLConnection conn = url.openConnection();
                    if ("text/html".equals(conn.getContentType())) {
                        ValidationError error = new ValidationError();
                        error.setMessage("Unable to access image");
                        error.addKey("imageUnavailable");
                        input.error(error);
                        return; // error message back!
                    }
                } catch (MalformedURLException e) {
                    ValidationError error = new ValidationError();
                    error.setMessage("Unable to access image");
                    error.addKey("imageUnavailable");
                    input.error(error);
                } catch (IOException e) {
                    ValidationError error = new ValidationError();
                    error.setMessage("Unable to access image");
                    error.addKey("imageUnavailable");
                    input.error(error);
                }
                return; // no further checks possible
            } else {
                GeoServerResourceLoader resources = GeoServerApplication.get().getResourceLoader();
                try {
                    File styles = resources.find("styles");
                    String[] path = value.split(Pattern.quote(File.separator));
                    WorkspaceInfo wsInfo = styleModel.getObject().getWorkspace();
                    File test = null;
                    if (wsInfo != null) {
                        String wsName = wsInfo.getName();
                        List<String> list = new ArrayList();
                        list.addAll(Arrays.asList("workspaces", wsName, "styles"));
                        list.addAll(Arrays.asList(path));
                        test = resources.find(list.toArray(new String[list.size()]));
                    }
                    if (test == null) {
                        test = resources.find(styles, path);
                    }
                    if (test == null) {
                        ValidationError error = new ValidationError();
                        error.setMessage("File not found in styles directory");
                        error.addKey("imageNotFound");
                        input.error(error);
                    }
                } catch (IOException e) {
                    ValidationError error = new ValidationError();
                    error.setMessage("File not found in styles directory");
                    error.addKey("imageNotFound");
                    input.error(error);
                }
            }
        }
    });
    onlineResource.setOutputMarkupId(true);
    table.add(onlineResource);

    // add the autofill button
    autoFill = new GeoServerAjaxFormLink("autoFill", styleForm) {
        @Override
        public void onClick(AjaxRequestTarget target, Form<?> form) {

            URLConnection conn = getExternalGraphic(target, form);
            if (conn == null) {
                ValidationError error = new ValidationError();
                error.setMessage("Unable to access image");
                error.addKey("imageUnavailable");
                onlineResource.error(error);
            } else {
                format.setModelValue(new String[] { conn.getContentType() });
                BufferedImage image;
                try {
                    image = ImageIO.read(conn.getInputStream());
                    width.setModelValue(new String[] { "" + image.getWidth() });
                    height.setModelValue(new String[] { "" + image.getHeight() });
                } catch (IOException e) {
                    e.printStackTrace();
                }
                target.add(format);
                target.add(width);
                target.add(height);
            }
        }
    };

    table.add(autoFill);

    format = new TextField<String>("format", styleModel.bind("legend.format"));
    format.setOutputMarkupId(true);
    table.add(format);

    width = new TextField<Integer>("width", styleModel.bind("legend.width"), Integer.class);
    width.add(RangeValidator.minimum(0));
    width.setRequired(true);
    width.setOutputMarkupId(true);
    table.add(width);

    height = new TextField<Integer>("height", styleModel.bind("legend.height"), Integer.class);
    height.add(RangeValidator.minimum(0));
    height.setRequired(true);
    height.setOutputMarkupId(true);
    table.add(height);

    table.add(new AttributeModifier("style", showhideStyleModel));

    container.add(table);

    showhideForm = new Form<StyleInfo>("showhide") {
        @Override
        protected void onSubmit() {
            super.onSubmit();
        }
    };
    showhideForm.setMarkupId("showhideForm");
    container.add(showhideForm);
    showhideForm.setMultiPart(true);

    show = new AjaxButton("show") {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            updateVisibility(true);
            target.add(ExternalGraphicPanel.this);
        }
    };
    container.add(show);
    showhideForm.add(show);

    hide = new AjaxButton("hide") {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
            onlineResource.setModelObject("");
            onlineResource.clearInput();
            format.setModelObject("");
            format.clearInput();
            width.setModelObject(0);
            width.clearInput();
            height.setModelObject(0);
            height.clearInput();

            updateVisibility(false);
            target.add(ExternalGraphicPanel.this);
        }
    };
    container.add(hide);
    showhideForm.add(hide);

    LegendInfo legend = styleModel.getObject().getLegend();
    boolean visible = legend != null && legend.getOnlineResource() != null
            && !legend.getOnlineResource().isEmpty();
    updateVisibility(visible);

}

From source file:de.bps.course.nodes.vc.provider.wimba.WimbaClassroomProvider.java

public URL createClassroomRecordingUrl(String recordingId, Identity identity) {
    URL url = null;//w  w w .j a v  a2s.com
    /* Notice: This is a very special and wimba specific case, the recordingId must not prefixed! */
    URI uri = UriBuilder.fromUri(protocol + "://" + baseUrl).port(port).path("check_wizard.pl")
            .queryParam("channel", recordingId).queryParam("hzA", token).build();
    try {
        url = uri.toURL();
    } catch (MalformedURLException e) {
        logWarn("Cannot create access URL to Wimba Classroom meeting for id \"" + recordingId + "\" and user \""
                + identity.getName() + "\" (" + identity.getKey() + ")", e);
    }

    return url;
}

From source file:com.googlecode.fascinator.indexer.SolrIndexer.java

/**
 * Initialize a Solr core object./*from  w  w  w. jav a2s  .co m*/
 * 
 * @param coreName : The core to initialize
 * @return SolrServer : The initialized core
 */
private SolrServer initCore(String coreName) {
    try {
        String uri = config.getString(null, "indexer", coreName, "uri");
        if (uri == null) {
            log.error("No URI provided for core: '{}'", coreName);
            return null;
        }
        URI solrUri = new URI(uri);
        CommonsHttpSolrServer thisCore = new CommonsHttpSolrServer(solrUri.toURL());
        String username = config.getString(null, "indexer", coreName, "username");
        String password = config.getString(null, "indexer", coreName, "password");
        usernameMap.put(coreName, username);
        passwordMap.put(coreName, password);
        if (username != null && password != null) {
            UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
            HttpClient hc = (thisCore).getHttpClient();
            hc.getParams().setAuthenticationPreemptive(true);
            hc.getState().setCredentials(AuthScope.ANY, credentials);
        }
        return thisCore;
    } catch (MalformedURLException mue) {
        log.error(coreName + " : Malformed URL", mue);
    } catch (URISyntaxException urise) {
        log.error(coreName + " : Invalid URI", urise);
    }
    return null;
}

From source file:de.bps.course.nodes.vc.provider.wimba.WimbaClassroomProvider.java

@Override
public URL createClassroomUrl(String roomId, Identity identity, VCConfiguration config) {
    URL url = null;/*from   w  ww.j  av  a  2 s. c  o m*/
    URI uri = UriBuilder.fromUri(protocol + "://" + baseUrl).port(port).path("check_wizard.pl")
            .queryParam("channel", PREFIX + roomId).queryParam("hzA", token).build();
    try {
        url = uri.toURL();
    } catch (MalformedURLException e) {
        logWarn("Cannot create access URL to Wimba Classroom meeting for id \"" + PREFIX + roomId
                + "\" and user \"" + identity.getName() + "\" (" + identity.getKey() + ")", e);
    }

    return url;
}

From source file:bixo.fetcher.SimpleHttpFetcher.java

private FetchedResult doRequest(HttpRequestBase request, String url, List<Tuple2<?, ?>> data,
        List<Tuple2<?, ?>> headers) throws BaseFetchException {
    LOGGER.trace("Fetching " + url);

    HttpResponse response;/*ww w  .j  ava  2 s.  c o  m*/
    long readStartTime;
    HttpHeaders headerMap = new HttpHeaders();
    String redirectedUrl = null;
    String newBaseUrl = null;
    int numRedirects = 0;
    boolean needAbort = true;
    String contentType = "";
    String hostAddress = null;

    // Create a local instance of cookie store, and bind to local context
    // Without this we get killed w/lots of threads, due to sync() on single
    // cookie store.
    HttpContext localContext = new BasicHttpContext();
    CookieStore cookieStore = new BasicCookieStore();
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    try {
        URI uri = new URI(url);
        request.setURI(uri);
        request.setHeader("Host", uri.getHost());

        if (headers != null) {
            for (Tuple2<?, ?> t : headers) {
                request.setHeader(t.getKey().toString(), t.getValue().toString());
            }
        }

        //collect post data if available
        if (request instanceof HttpPost && data != null) {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            for (Tuple2<?, ?> e : data) {
                nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode(e.getKey().toString(), "utf-8"),
                        URLEncoder.encode(e.getValue().toString(), "utf-8")));
            }
            ((HttpPost) (request)).setEntity(new UrlEncodedFormEntity(nameValuePairs));
        }

        readStartTime = System.currentTimeMillis();
        response = _httpClient.execute(request, localContext);

        Header[] responseHeaders = response.getAllHeaders();
        for (Header header : responseHeaders) {
            headerMap.add(header.getName(), header.getValue());
        }

        int httpStatus = response.getStatusLine().getStatusCode();
        if ((httpStatus < 200) || (httpStatus >= 300)) {
            // We can't just check against SC_OK, as some wackos return 201, 202,
            // etc
            throw new HttpFetchException(url,
                    "Error fetching " + url + " due to http status code " + httpStatus, httpStatus, headerMap);
        }

        redirectedUrl = extractRedirectedUrl(url, localContext);

        URI permRedirectUri = (URI) localContext.getAttribute(PERM_REDIRECT_CONTEXT_KEY);
        if (permRedirectUri != null) {
            newBaseUrl = permRedirectUri.toURL().toExternalForm();
        }

        Integer redirects = (Integer) localContext.getAttribute(REDIRECT_COUNT_CONTEXT_KEY);
        if (redirects != null) {
            numRedirects = redirects.intValue();
        }

        hostAddress = (String) (localContext.getAttribute(HOST_ADDRESS));
        if (hostAddress == null) {
            throw new UrlFetchException(url, "Host address not saved in context");
        }

        Header cth = response.getFirstHeader(HttpHeaderNames.CONTENT_TYPE);
        if (cth != null) {
            contentType = cth.getValue();
        }

        needAbort = false;
    } catch (ClientProtocolException e) {
        // Oleg guarantees that no abort is needed in the case of an IOException
        // (which is is a subclass of)
        needAbort = false;

        // If the root case was a "too many redirects" error, we want to map this
        // to a specific
        // exception that contains the final redirect.
        if (e.getCause() instanceof MyRedirectException) {
            MyRedirectException mre = (MyRedirectException) e.getCause();
            String redirectUrl = url;

            try {
                redirectUrl = mre.getUri().toURL().toExternalForm();
            } catch (MalformedURLException e2) {
                LOGGER.warn("Invalid URI saved during redirect handling: " + mre.getUri());
            }

            throw new RedirectFetchException(url, redirectUrl, mre.getReason());
        } else if (e.getCause() instanceof RedirectException) {
            throw new RedirectFetchException(url, extractRedirectedUrl(url, localContext),
                    RedirectExceptionReason.TOO_MANY_REDIRECTS);
        } else {
            throw new IOFetchException(url, e);
        }
    } catch (IOException e) {
        // Oleg guarantees that no abort is needed in the case of an IOException
        needAbort = false;

        if (e instanceof ConnectionPoolTimeoutException) {
            // Should never happen, so let's dump some info about the connection
            // pool.
            ThreadSafeClientConnManager cm = (ThreadSafeClientConnManager) _httpClient.getConnectionManager();
            int numConnections = cm.getConnectionsInPool();
            cm.closeIdleConnections(0, TimeUnit.MILLISECONDS);
            LOGGER.error(String.format(
                    "Got ConnectionPoolTimeoutException: %d connections before, %d after idle close",
                    numConnections, cm.getConnectionsInPool()));
        }

        throw new IOFetchException(url, e);
    } catch (URISyntaxException e) {
        throw new UrlFetchException(url, e.getMessage());
    } catch (IllegalStateException e) {
        throw new UrlFetchException(url, e.getMessage());
    } catch (BaseFetchException e) {
        throw e;
    } catch (Exception e) {
        // Map anything else to a generic IOFetchException
        // TODO KKr - create generic fetch exception
        throw new IOFetchException(url, new IOException(e));
    } finally {
        safeAbort(needAbort, request);
    }

    // Figure out how much data we want to try to fetch.
    int targetLength = _fetcherPolicy.getMaxContentSize();
    boolean truncated = false;
    String contentLengthStr = headerMap.getFirst(HttpHeaderNames.CONTENT_LENGTH);
    if (contentLengthStr != null) {
        try {
            int contentLength = Integer.parseInt(contentLengthStr);
            if (contentLength > targetLength) {
                truncated = true;
            } else {
                targetLength = contentLength;
            }
        } catch (NumberFormatException e) {
            // Ignore (and log) invalid content length values.
            LOGGER.warn("Invalid content length in header: " + contentLengthStr);
        }
    }

    // Now finally read in response body, up to targetLength bytes.
    // Note that entity might be null, for zero length responses.
    byte[] content = new byte[0];
    long readRate = 0;
    HttpEntity entity = response.getEntity();
    needAbort = true;

    if (entity != null) {
        InputStream in = null;

        try {
            in = entity.getContent();
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = 0;
            int totalRead = 0;
            ByteArrayOutputStream out = new ByteArrayOutputStream(DEFAULT_BYTEARRAY_SIZE);

            int readRequests = 0;
            int minResponseRate = _fetcherPolicy.getMinResponseRate();
            // TODO KKr - we need to monitor the rate while reading a
            // single block. Look at HttpClient
            // metrics support for how to do this. Once we fix this, fix
            // the test to read a smaller (< 20K)
            // chuck of data.
            while ((totalRead < targetLength) && ((bytesRead = in.read(buffer, 0,
                    Math.min(buffer.length, targetLength - totalRead))) != -1)) {
                readRequests += 1;
                totalRead += bytesRead;
                out.write(buffer, 0, bytesRead);

                // Assume read time is at least one millisecond, to avoid DBZ
                // exception.
                long totalReadTime = Math.max(1, System.currentTimeMillis() - readStartTime);
                readRate = (totalRead * 1000L) / totalReadTime;

                // Don't bail on the first read cycle, as we can get a hiccup starting
                // out.
                // Also don't bail if we've read everything we need.
                if ((readRequests > 1) && (totalRead < targetLength) && (readRate < minResponseRate)) {
                    throw new AbortedFetchException(url, "Slow response rate of " + readRate + " bytes/sec",
                            AbortedFetchReason.SLOW_RESPONSE_RATE);
                }

                // Check to see if we got interrupted.
                if (Thread.interrupted()) {
                    throw new AbortedFetchException(url, AbortedFetchReason.INTERRUPTED);
                }
            }

            content = out.toByteArray();
            needAbort = truncated || (in.available() > 0);
        } catch (IOException e) {
            // We don't need to abort if there's an IOException
            throw new IOFetchException(url, e);
        } finally {
            safeAbort(needAbort, request);
            safeClose(in);
        }
    }

    return new FetchedResult(url, redirectedUrl, System.currentTimeMillis(), headerMap, content, contentType,
            (int) readRate, newBaseUrl, numRedirects, hostAddress);
}