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:com.google.zxing.web.DecodeServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String imageURIString = request.getParameter("u");
    if (imageURIString == null || imageURIString.isEmpty()) {
        log.info("URI was empty");
        response.sendRedirect("badurl.jspx");
        return;//  ww w  .  j  a  va2  s.c om
    }

    imageURIString = imageURIString.trim();
    for (CharSequence substring : blockedURLSubstrings) {
        if (imageURIString.contains(substring)) {
            log.info("Disallowed URI " + imageURIString);
            response.sendRedirect("badurl.jspx");
            return;
        }
    }

    URI imageURI;
    try {
        imageURI = new URI(imageURIString);
        // Assume http: if not specified
        if (imageURI.getScheme() == null) {
            imageURI = new URI("http://" + imageURIString);
        }
    } catch (URISyntaxException urise) {
        log.info("URI " + imageURIString + " was not valid: " + urise);
        response.sendRedirect("badurl.jspx");
        return;
    }

    // Shortcut for data URI
    if ("data".equals(imageURI.getScheme())) {
        try {
            BufferedImage image = ImageReader.readDataURIImage(imageURI);
            processImage(image, request, response);
        } catch (IOException ioe) {
            log.info(ioe.toString());
            response.sendRedirect("badurl.jspx");
        }
        return;
    }

    URL imageURL;
    try {
        imageURL = imageURI.toURL();
    } catch (MalformedURLException ignored) {
        log.info("URI was not valid: " + imageURIString);
        response.sendRedirect("badurl.jspx");
        return;
    }

    String protocol = imageURL.getProtocol();
    if (!"http".equalsIgnoreCase(protocol) && !"https".equalsIgnoreCase(protocol)) {
        log.info("URI was not valid: " + imageURIString);
        response.sendRedirect("badurl.jspx");
        return;
    }

    HttpURLConnection connection;
    try {
        connection = (HttpURLConnection) imageURL.openConnection();
    } catch (IllegalArgumentException ignored) {
        log.info("URI could not be opened: " + imageURL);
        response.sendRedirect("badurl.jspx");
        return;
    }

    connection.setAllowUserInteraction(false);
    connection.setReadTimeout(5000);
    connection.setConnectTimeout(5000);
    connection.setRequestProperty(HttpHeaders.USER_AGENT, "zxing.org");
    connection.setRequestProperty(HttpHeaders.CONNECTION, "close");

    try {
        connection.connect();
    } catch (IOException ioe) {
        // Encompasses lots of stuff, including
        //  java.net.SocketException, java.net.UnknownHostException,
        //  javax.net.ssl.SSLPeerUnverifiedException,
        //  org.apache.http.NoHttpResponseException,
        //  org.apache.http.client.ClientProtocolException,
        log.info(ioe.toString());
        response.sendRedirect("badurl.jspx");
        return;
    }

    try (InputStream is = connection.getInputStream()) {
        try {
            if (connection.getResponseCode() != HttpServletResponse.SC_OK) {
                log.info("Unsuccessful return code: " + connection.getResponseCode());
                response.sendRedirect("badurl.jspx");
                return;
            }
            if (connection.getHeaderFieldInt(HttpHeaders.CONTENT_LENGTH, 0) > MAX_IMAGE_SIZE) {
                log.info("Too large");
                response.sendRedirect("badimage.jspx");
                return;
            }

            log.info("Decoding " + imageURL);
            processStream(is, request, response);
        } finally {
            consumeRemainder(is);
        }
    } catch (IOException ioe) {
        log.info(ioe.toString());
        response.sendRedirect("badurl.jspx");
    } finally {
        connection.disconnect();
    }

}

From source file:org.kitodo.production.helper.metadata.ImageHelper.java

/**
 * scale given image file to png using internal embedded content server.
 *//*  w  w  w .  j a  v a 2  s.c o  m*/
public void scaleFile(URI inFileName, URI outFileName, int inSize, int intRotation)
        throws ImageManagerException, IOException, ImageManipulatorException {
    logger.trace("start scaleFile");
    int tmpSize = inSize / 3;
    if (tmpSize < 1) {
        tmpSize = 1;
    }
    logger.trace("tmpSize: {}", tmpSize);
    Optional<String> kitodoContentServerUrl = ConfigCore
            .getOptionalString(ParameterCore.KITODO_CONTENT_SERVER_URL);
    if (kitodoContentServerUrl.isPresent()) {
        if (kitodoContentServerUrl.get().isEmpty()) {
            logger.trace("api");
            // TODO source image files are locked under windows forever after
            // converting to png begins.
            ImageManager imageManager = new ImageManager(inFileName.toURL());
            logger.trace("im");
            RenderedImage renderedImage = imageManager.scaleImageByPixel(tmpSize, tmpSize,
                    ImageManager.SCALE_BY_PERCENT, intRotation);
            logger.trace("ri");
            JpegInterpreter jpegInterpreter = new JpegInterpreter(renderedImage);
            logger.trace("pi");
            FileOutputStream outputFileStream = (FileOutputStream) fileService.write(outFileName);
            logger.trace("output");
            jpegInterpreter.writeToStream(null, outputFileStream);
            logger.trace("write stream");
            outputFileStream.flush();
            outputFileStream.close();
            logger.trace("close stream");
        } else {
            String cs = kitodoContentServerUrl.get() + inFileName + "&scale=" + tmpSize + "&rotate="
                    + intRotation + "&format=jpg";
            cs = cs.replace("\\", "/");
            logger.trace("url: {}", cs);
            URL csUrl = new URL(cs);
            HttpClient httpclient = new HttpClient();
            GetMethod method = new GetMethod(csUrl.toString());
            logger.trace("get");
            Integer contentServerTimeOut = ConfigCore
                    .getIntParameterOrDefaultValue(ParameterCore.KITODO_CONTENT_SERVER_TIMEOUT);
            method.getParams().setParameter("http.socket.timeout", contentServerTimeOut);
            int statusCode = httpclient.executeMethod(method);
            if (statusCode != HttpStatus.SC_OK) {
                return;
            }
            logger.trace("statusCode: {}", statusCode);
            InputStream inStream = method.getResponseBodyAsStream();
            logger.trace("inStream");
            try (BufferedInputStream bis = new BufferedInputStream(inStream);
                    OutputStream fos = fileService.write(outFileName)) {
                logger.trace("BufferedInputStream");
                logger.trace("FileOutputStream");
                byte[] bytes = new byte[8192];
                int count = bis.read(bytes);
                while (count != -1 && count <= 8192) {
                    fos.write(bytes, 0, count);
                    count = bis.read(bytes);
                }
                if (count != -1) {
                    fos.write(bytes, 0, count);
                }
            }
            logger.trace("write");
            inStream.close();
        }
        logger.trace("end scaleFile");
    }
}

From source file:nl.minbzk.dwr.zoeken.enricher.processor.TikaProcessor.java

/**
 * Transformt the given input URI into an InputStream.
 *
 * @param inputUri/*  w w  w  .  j  a va  2  s. c o  m*/
 * @param file
 * @return InputStream
 * @throws Exception
 */
private InputStream readInputUri(final String inputUri, final File file) throws Exception {
    InputStream inputStream;

    if (file.isFile()) {
        inputStream = new FileInputStream(file);

        if (logger.isInfoEnabled())
            logger.info("Fetching given input file " + inputUri);
    } else {
        try {
            URI uri = new URI(inputUri);

            if (inputUri.startsWith("data://")) {
                if (logger.isInfoEnabled())
                    logger.info("Fetching data URI with ID " + inputUri.substring(7));

                inputStream = retrieveGridStream(inputUri.substring(7));
            } else {
                if (logger.isInfoEnabled())
                    logger.info("Fetching given input URL " + inputUri);

                inputStream = uri.toURL().openStream();
            }
        } catch (URISyntaxException e) {
            throw new Exception("The given input URI is neither an existing file nor a valid URL.");
        }
    }

    if (!inputStream.markSupported())
        inputStream = new BufferedInputStream(inputStream);
    return inputStream;
}

From source file:com.vmware.appfactory.recipe.controller.RecipeApiController.java

private void writeRecipeFileToZip(Recipe recipe, RecipeFile file, ZipOutputStream zos) throws IOException {
    _log.debug("Writing recipe payload file {}, URI = {}", file.getPath(), file.getURI().toString());

    URI uri = file.getURI();
    InputStream is = null;/*from   w  ww. ja v  a  2  s. c  o  m*/

    try {
        zos.putNextEntry(new ZipEntry(file.getPath()));

        if (uri.getScheme().equals(DsUtil.DATASTORE_URI_SCHEME)) {
            /*
             * Use datastore methods to copy the file from the datastore
             * into the ZIP file.
             */
            Long dsId = Long.valueOf(uri.getHost());
            DsDatastore ds = _dsClient.findDatastore(dsId, true);
            if (ds == null) {
                throw new URISyntaxException(uri.toString(), "Datastore URI has invalid ID " + uri.getHost());
            }

            is = ds.openStream(uri.getPath());
            IOUtils.copy(is, zos);
        } else {
            /*
             * Use regular HTTP methods to copy file from URL into the ZIP file.
             */
            URL url = uri.toURL();
            is = new BufferedInputStream(url.openStream());
            IOUtils.copy(is, zos);
        }
    } catch (URISyntaxException ex) {
        throwBadLocationException(recipe, uri.toString());
    } catch (DsException ex) {
        throwBadLocationException(recipe, uri.toString());
    } catch (MalformedURLException ex) {
        throwBadLocationException(recipe, uri.toString());
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.ibm.jaggr.service.impl.AggregatorImpl.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {
    if (log.isLoggable(Level.FINEST))
        log.finest("doGet: URL=" + req.getRequestURI()); //$NON-NLS-1$

    req.setAttribute(AGGREGATOR_REQATTRNAME, this);
    ConcurrentMap<String, Object> concurrentMap = new ConcurrentHashMap<String, Object>();
    req.setAttribute(CONCURRENTMAP_REQATTRNAME, concurrentMap);

    try {/*from   w w  w.  ja  v  a 2  s.c  o  m*/
        // Validate config last-modified if development mode is enabled
        if (getOptions().isDevelopmentMode()) {
            long lastModified = -1;
            URI configUri = getConfig().getConfigUri();
            if (configUri != null) {
                lastModified = configUri.toURL().openConnection().getLastModified();
            }
            if (lastModified > getConfig().lastModified()) {
                if (reloadConfig()) {
                    // If the config has been modified, then dependencies will be revalidated
                    // asynchronously.  Rather than forcing the current request to wait, return
                    // a response that will display an alert informing the user of what is 
                    // happening and asking them to reload the page.
                    String content = "alert('" + //$NON-NLS-1$ 
                            StringUtil.escapeForJavaScript(Messages.ConfigModified) + "');"; //$NON-NLS-1$
                    resp.addHeader("Cache-control", "no-store"); //$NON-NLS-1$ //$NON-NLS-2$
                    CopyUtil.copy(new StringReader(content), resp.getOutputStream());
                    return;
                }
            }
        }

        getTransport().decorateRequest(req);
        notifyRequestListeners(RequestNotifierAction.start, req, resp);

        ILayer layer = getLayer(req);
        long modifiedSince = req.getDateHeader("If-Modified-Since"); //$NON-NLS-1$
        long lastModified = (Math.max(getCacheManager().getCache().getCreated(), layer.getLastModified(req))
                / 1000) * 1000;
        if (modifiedSince >= lastModified) {
            if (log.isLoggable(Level.FINER)) {
                log.finer("Returning Not Modified response for layer in servlet" + //$NON-NLS-1$
                        getName() + ":" //$NON-NLS-1$
                        + req.getAttribute(IHttpTransport.REQUESTEDMODULES_REQATTRNAME).toString());
            }
            resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        } else {
            // Get the InputStream for the response.  This call sets the Content-Type,
            // Content-Length and Content-Encoding headers in the response.
            InputStream in = layer.getInputStream(req, resp);
            // if any of the readers included an error response, then don't cache the layer.
            if (req.getAttribute(ILayer.NOCACHE_RESPONSE_REQATTRNAME) != null) {
                resp.addHeader("Cache-Control", "no-store"); //$NON-NLS-1$ //$NON-NLS-2$
            } else {
                resp.setDateHeader("Last-Modified", lastModified); //$NON-NLS-1$
                int expires = getConfig().getExpires();
                if (expires > 0) {
                    resp.addHeader("Cache-Control", "max-age=" + expires); //$NON-NLS-1$ //$NON-NLS-2$
                }
            }
            CopyUtil.copy(in, resp.getOutputStream());
        }
        notifyRequestListeners(RequestNotifierAction.end, req, resp);
    } catch (DependencyVerificationException e) {
        // clear the cache now even though it will be cleared when validateDeps has 
        // finished (asynchronously) so that any new requests will be forced to wait
        // until dependencies have been validated.
        getCacheManager().clearCache();
        getDependencies().validateDeps(false);

        resp.addHeader("Cache-control", "no-store"); //$NON-NLS-1$ //$NON-NLS-2$
        if (getOptions().isDevelopmentMode()) {
            String msg = StringUtil.escapeForJavaScript(MessageFormat.format(Messages.DepVerificationFailed,
                    new Object[] { e.getMessage(), AggregatorCommandProvider.EYECATCHER + " " + //$NON-NLS-1$
                            AggregatorCommandProvider.CMD_VALIDATEDEPS + " " + //$NON-NLS-1$
                            getName() + " " + //$NON-NLS-1$
                            AggregatorCommandProvider.PARAM_CLEAN,
                            getWorkingDirectory().toString().replace("\\", "\\\\") //$NON-NLS-1$ //$NON-NLS-2$
                    }));
            String content = "alert('" + msg + "');"; //$NON-NLS-1$ //$NON-NLS-2$
            try {
                CopyUtil.copy(new StringReader(content), resp.getOutputStream());
            } catch (IOException e1) {
                if (log.isLoggable(Level.SEVERE)) {
                    log.log(Level.SEVERE, e1.getMessage(), e1);
                }
                resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        } else {
            resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        }
    } catch (ProcessingDependenciesException e) {
        resp.addHeader("Cache-control", "no-store"); //$NON-NLS-1$ //$NON-NLS-2$
        if (getOptions().isDevelopmentMode()) {
            String content = "alert('" + StringUtil.escapeForJavaScript(Messages.Busy) + "');"; //$NON-NLS-1$ //$NON-NLS-2$
            try {
                CopyUtil.copy(new StringReader(content), resp.getOutputStream());
            } catch (IOException e1) {
                if (log.isLoggable(Level.SEVERE)) {
                    log.log(Level.SEVERE, e1.getMessage(), e1);
                }
                resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            }
        } else {
            resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        }
    } catch (BadRequestException e) {
        exceptionResponse(req, resp, e, HttpServletResponse.SC_BAD_REQUEST);
    } catch (NotFoundException e) {
        exceptionResponse(req, resp, e, HttpServletResponse.SC_NOT_FOUND);
    } catch (Exception e) {
        exceptionResponse(req, resp, e, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } finally {
        concurrentMap.clear();
    }
}

From source file:org.corpus_tools.pepper.connectors.impl.PepperOSGiConnector.java

/**
 * Installs the given bundle and copies it to the plugin path, but does not
 * start it. <br>// w ww.  j  a v a  2s.com
 * If the the URI is of scheme http or https, the file will be downloaded.
 * <br/>
 * If the URI points to a zip file, it will be extracted and copied.
 * 
 * @param bundleURI
 * @return
 * @throws BundleException
 * @throws IOException
 */
public Bundle installAndCopy(URI bundleURI) throws BundleException, IOException {
    Bundle retVal = null;
    if (bundleURI != null) {
        String pluginPath = getPepperStarterConfiguration().getPlugInPath();
        if (pluginPath != null) {
            // download file, if file is a web resource
            if (("http".equalsIgnoreCase(bundleURI.getScheme()))
                    || ("https".equalsIgnoreCase(bundleURI.getScheme()))) {
                String tempPath = getPepperStarterConfiguration().getTempPath().getCanonicalPath();
                URL bundleUrl = bundleURI.toURL();
                if (!tempPath.endsWith("/")) {
                    tempPath = tempPath + "/";
                }
                String baseName = FilenameUtils.getBaseName(bundleUrl.toString());
                String extension = FilenameUtils.getExtension(bundleUrl.toString());
                File bundleFile = new File(tempPath + baseName + "." + extension);

                org.apache.commons.io.FileUtils.copyURLToFile(bundleURI.toURL(), bundleFile);
                bundleURI = URI.create(bundleFile.getAbsolutePath());
            }
            if (bundleURI.getPath().endsWith("zip")) {
                try (ZipFile zipFile = new ZipFile(bundleURI.getPath());) {
                    Enumeration<? extends ZipEntry> entries = zipFile.entries();
                    while (entries.hasMoreElements()) {
                        ZipEntry entry = entries.nextElement();
                        File entryDestination = new File(pluginPath, entry.getName());
                        if (!entryDestination.getParentFile().exists()
                                && !entryDestination.getParentFile().mkdirs()) {
                            logger.warn("Cannot create folder '" + entryDestination.getParentFile() + "'. ");
                        }
                        if (entry.isDirectory()) {
                            if (!entryDestination.getParentFile().exists()
                                    && !entryDestination.getParentFile().mkdirs()) {
                                logger.warn("Cannot create folder {}. ", entryDestination.getParentFile());
                            }
                        } else {
                            InputStream in = zipFile.getInputStream(entry);
                            OutputStream out = new FileOutputStream(entryDestination);
                            IOUtils.copy(in, out);
                            IOUtils.closeQuietly(in);
                            IOUtils.closeQuietly(out);
                            if (entryDestination.getName().endsWith(".jar")) {
                                retVal = install(entryDestination.toURI());
                            }
                        }
                    }
                }
            } else if (bundleURI.getPath().endsWith("jar")) {
                File bundleFile = new File(bundleURI.getPath());
                File jarFile = new File(pluginPath, bundleFile.getName());
                FileUtils.copyFile(bundleFile, jarFile);
                retVal = install(jarFile.toURI());
            }
        }
    }

    return (retVal);
}

From source file:crawlercommons.fetcher.http.SimpleHttpFetcher.java

private FetchedResult doRequest(HttpRequestBase request, String url, Payload payload)
        throws BaseFetchException {
    LOGGER.trace("Fetching " + url);

    HttpResponse response;/*  w w w.  j  a v  a2s. c  o m*/
    long readStartTime;
    Metadata headerMap = new Metadata();
    String redirectedUrl = null;
    String newBaseUrl = null;
    int numRedirects = 0;
    boolean needAbort = true;
    String contentType = "";
    String mimeType = "";
    String hostAddress = null;
    int statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    String reasonPhrase = 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 = localCookieStore.get();
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

    StringBuilder fetchTrace = null;
    if (LOGGER.isTraceEnabled()) {
        fetchTrace = new StringBuilder("Fetched url: " + url);
    }

    try {
        request.setURI(new URI(url));

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

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

        statusCode = response.getStatusLine().getStatusCode();
        reasonPhrase = response.getStatusLine().getReasonPhrase();

        if (LOGGER.isTraceEnabled()) {
            fetchTrace.append("; status code: " + statusCode);
            if (headerMap.get(HttpHeaders.CONTENT_LENGTH) != null) {
                fetchTrace.append("; Content-Length: " + headerMap.get(HttpHeaders.CONTENT_LENGTH));
            }

            if (headerMap.get(HttpHeaders.LOCATION) != null) {
                fetchTrace.append("; Location: " + headerMap.get(HttpHeaders.LOCATION));
            }
        }

        if ((statusCode < 200) || (statusCode >= 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 \"" + reasonPhrase + "\"",
                    statusCode, 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(HttpHeaders.CONTENT_TYPE);
        if (cth != null) {
            contentType = cth.getValue();
        }

        // Check if we should abort due to mime-type filtering. Note that
        // this will fail if the server
        // doesn't report a mime-type, but that's how we want it as this
        // configuration is typically
        // used when only a subset of parsers are installed/enabled, so we
        // don't want the auto-detect
        // code in Tika to get triggered & try to process an unsupported
        // type. If you want unknown
        // mime-types from the server to be processed, set "" as one of the
        // valid mime-types in
        // FetcherPolicy.
        mimeType = getMimeTypeFromContentType(contentType);
        Set<String> mimeTypes = getValidMimeTypes();
        if ((mimeTypes != null) && (mimeTypes.size() > 0)) {
            if (!mimeTypes.contains(mimeType)) {
                throw new AbortedFetchException(url, "Invalid mime-type: " + mimeType,
                        AbortedFetchReason.INVALID_MIMETYPE);
            }
        }

        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) {
            LOGGER.error(e.getMessage());
            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;
        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 maxContentSize = getMaxContentSize(mimeType);
    int targetLength = maxContentSize;
    boolean truncated = false;
    String contentLengthStr = headerMap.get(HttpHeaders.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 = 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, but don't clear the
                // interrupted flag.
                if (Thread.currentThread().isInterrupted()) {
                    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);
        }
    }

    // Toss truncated image content.
    if ((truncated) && (!isTextMimeType(mimeType))) {
        throw new AbortedFetchException(url, "Truncated image", AbortedFetchReason.CONTENT_SIZE);
    }

    // Now see if we need to uncompress the content.
    String contentEncoding = headerMap.get(HttpHeaders.CONTENT_ENCODING);
    if (contentEncoding != null) {
        if (LOGGER.isTraceEnabled()) {
            fetchTrace.append("; Content-Encoding: " + contentEncoding);
        }

        // TODO KKr We might want to just decompress a truncated gzip
        // containing text (since we have a max content size to save us
        // from any gzip corruption). We might want to break the following
        // out into a separate method, by the way (if not refactor this
        // entire monolithic method).
        //
        try {
            if ("gzip".equals(contentEncoding) || "x-gzip".equals(contentEncoding)) {
                if (truncated) {
                    throw new AbortedFetchException(url, "Truncated compressed data",
                            AbortedFetchReason.CONTENT_SIZE);
                } else {
                    ExpandedResult expandedResult = EncodingUtils.processGzipEncoded(content, maxContentSize);
                    truncated = expandedResult.isTruncated();
                    if ((truncated) && (!isTextMimeType(mimeType))) {
                        throw new AbortedFetchException(url, "Truncated decompressed image",
                                AbortedFetchReason.CONTENT_SIZE);
                    } else {
                        content = expandedResult.getExpanded();
                        if (LOGGER.isTraceEnabled()) {
                            fetchTrace.append("; unzipped to " + content.length + " bytes");
                        }
                    }
                    // } else if ("deflate".equals(contentEncoding)) {
                    // content =
                    // EncodingUtils.processDeflateEncoded(content);
                    // if (LOGGER.isTraceEnabled()) {
                    // fetchTrace.append("; inflated to " + content.length +
                    // " bytes");
                    // }
                }
            }
        } catch (IOException e) {
            throw new IOFetchException(url, e);
        }
    }

    // Finally dump out the trace msg we've been building.
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(fetchTrace.toString());
    }

    // TODO KKr - Save truncated flag in FetchedResult/FetchedDatum.
    return new FetchedResult(url, redirectedUrl, System.currentTimeMillis(), headerMap, content, contentType,
            (int) readRate, payload, newBaseUrl, numRedirects, hostAddress, statusCode, reasonPhrase);
}

From source file:org.craftercms.studio.impl.v1.repository.alfresco.AlfrescoContentRepository.java

/**
 * fire GET request to Alfresco with proper security
 *//*from   w w w.ja v  a 2s  .c  om*/
protected InputStream alfrescoGetRequest(String uri, Map<String, String> params) throws Exception {
    long startTime = System.currentTimeMillis();
    InputStream retResponse = null;

    URI serviceURI = new URI(buildAlfrescoRequestURL(uri, params));

    retResponse = serviceURI.toURL().openStream();

    long duration = System.currentTimeMillis() - startTime;
    logger.debug("alfrescoGetRequest(String uri, Map<String, String> params); {0}, {1}\n\t\tDuration: {2}", uri,
            params.values(), duration);
    return retResponse;
}

From source file:eu.forgestore.ws.repo.FStoreRepositoryAPIImpl.java

@POST
//@Path("/users/{userid}/widgets/packaged")
@Path("/admin/widgets/packaged")
@Consumes("multipart/form-data")
@Produces("application/json")
public Response addWidgetPackaged(List<Attachment> ats) {

    //      MUST STORE Sessions and the from Session ID to get userid
    //      must show only widges owned by user Session. must find user from sessionid

    FStoreUser u = fstoreRepositoryRef/*from   w w w.  j  a va2 s  .  c o m*/
            .getUserBySessionId(SecurityUtils.getSubject().getSession().getId().toString());
    int userid = u.getId();

    Widget widg = new Widget();
    widg.setURL(getAttachmentStringValue("url", ats));
    widg = (Widget) addNewProductData(widg, userid, getAttachmentStringValue("prodname", ats),
            getAttachmentStringValue("shortDescription", ats), getAttachmentStringValue("longDescription", ats),
            getAttachmentStringValue("version", ats), getAttachmentStringValue("categories", ats), //categories are comma separated Ids
            getAttachmentByName("prodIcon", ats), getAttachmentByName("prodFile", ats),
            getListOfAttachmentsByName("screenshots", ats));

    Attachment widgetPackagedFile = getAttachmentByName("prodFile", ats);
    if (widgetPackagedFile != null) {
        String widgetFileNamePosted = getFileName(widgetPackagedFile.getHeaders());
        logger.info("widgetFileNamePosted = " + widgetFileNamePosted);

        if (!widgetFileNamePosted.equals("")) {

            File destFile = new File(METADATADIR + widg.getUuid() + File.separator + widgetFileNamePosted);
            java.nio.file.Path targetPath = destFile.toPath();

            String cmdStr = "tar --strip-components=1 -xvzf " + targetPath + " -C " + targetPath.getParent()
                    + File.separator;

            logger.info("cmdStr = " + cmdStr);

            Utils.executeSystemCommand(cmdStr);

        }
    }

    URI endpointUrl = uri.getBaseUri();

    String endpointUrlString;
    try {
        endpointUrlString = endpointUrl.toURL().getProtocol() + "://" + endpointUrl.toURL().getHost();
        if (endpointUrl.toURL().getPort() > 0) {
            endpointUrlString += ":" + endpointUrl.toURL().getPort();
        }
        String tempDir = endpointUrlString + File.separator + "static" + File.separator + widg.getUuid()
                + File.separator;
        widg.setURL(tempDir);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    fstoreRepositoryRef.updateProductInfo(widg);
    return Response.ok().entity(widg).build();
}

From source file:org.keycloak.testsuite.adapter.servlet.DemoServletsAdapterTest.java

@Test
public void testCallURLWithAccessToken() throws Exception {
    // test login to customer-portal which does a bearer request to customer-db
    URI applicationURL = inputPortalNoAccessToken.getUriBuilder().clone()
            .queryParam("access_token", "invalid_token").build();

    driver.navigate().to(applicationURL.toURL());

    assertEquals(applicationURL.toASCIIString(), driver.getCurrentUrl());
    inputPortalNoAccessToken.execute("hello");
    assertCurrentUrlStartsWithLoginUrlOf(testRealmPage);
}