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:edu.mayo.informatics.lexgrid.convert.utility.ManifestUtil.java

/**
 * This method validates and returns the manifest object for the
 * manifestURI_. Returns null if the manifestURI is invalid or the manifest
 * is invalid.//from w ww  . j  av  a  2s.c  o m
 * 
 * @param ontologyNameSpace
 * @return
 * @throws LgConvertException
 */
public CodingSchemeManifest getManifest(URI uri) {

    CodingSchemeManifest manifest = null;
    boolean valid = isValidFile(uri);
    String schema = "http://LexGrid.org/schema/2010/01/LexOnt/CodingSchemeManifest.xsd";
    if (valid) {
        try {
            org.exolab.castor.xml.Unmarshaller um = new org.exolab.castor.xml.Unmarshaller(
                    CodingSchemeManifest.class);
            boolean validating = um.isValidating();
            manifest = (CodingSchemeManifest) um
                    .unmarshal(new InputStreamReader(uri.toURL().openConnection().getInputStream()));

            StringWriter myWriter = new StringWriter();
            Marshaller m1 = new Marshaller(myWriter);
            m1.setNamespaceMapping("", schema);
            m1.setSchemaLocation("http://LexGrid.org/schema/2010/01/LexOnt/CodingSchemeManifest" + schema);
            m1.marshal(manifest);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    return manifest;
}

From source file:net.pms.dlna.RootFolder.java

/**
 * Creates, populates and returns a virtual folder mirroring the
 * contents of the system's iPhoto folder.
 * Mac OS X only./*from w  w  w.  ja v  a 2  s .c o m*/
 *
 * @return iPhotoVirtualFolder the populated <code>VirtualFolder</code>, or null if one couldn't be created.
 */
private DLNAResource getiPhotoFolder() {
    VirtualFolder iPhotoVirtualFolder = null;

    if (Platform.isMac()) {
        logger.debug("Adding iPhoto folder");
        InputStream inputStream = null;

        try {
            // This command will show the XML files for recently opened iPhoto databases
            Process process = Runtime.getRuntime().exec("defaults read com.apple.iApps iPhotoRecentDatabases");
            inputStream = process.getInputStream();
            List<String> lines = IOUtils.readLines(inputStream);
            logger.debug("iPhotoRecentDatabases: {}", lines);

            if (lines.size() >= 2) {
                // we want the 2nd line
                String line = lines.get(1);

                // Remove extra spaces
                line = line.trim();

                // Remove quotes
                line = line.substring(1, line.length() - 1);

                URI uri = new URI(line);
                URL url = uri.toURL();
                File file = FileUtils.toFile(url);
                logger.debug("Resolved URL to file: {} -> {}", url, file.getAbsolutePath());

                // Load the properties XML file.
                Map<String, Object> iPhotoLib = Plist.load(file);

                // The list of all photos
                Map<?, ?> photoList = (Map<?, ?>) iPhotoLib.get("Master Image List");

                // The list of events (rolls)
                List<Map<?, ?>> listOfRolls = (List<Map<?, ?>>) iPhotoLib.get("List of Rolls");

                iPhotoVirtualFolder = new VirtualFolder("iPhoto Library", null);

                for (Map<?, ?> roll : listOfRolls) {
                    Object rollName = roll.get("RollName");

                    if (rollName != null) {
                        VirtualFolder virtualFolder = new VirtualFolder(rollName.toString(), null);

                        // List of photos in an event (roll)
                        List<?> rollPhotos = (List<?>) roll.get("KeyList");

                        for (Object photo : rollPhotos) {
                            Map<?, ?> photoProperties = (Map<?, ?>) photoList.get(photo);

                            if (photoProperties != null) {
                                Object imagePath = photoProperties.get("ImagePath");

                                if (imagePath != null) {
                                    RealFile realFile = new RealFile(new File(imagePath.toString()));
                                    virtualFolder.addChild(realFile);
                                }
                            }
                        }

                        iPhotoVirtualFolder.addChild(virtualFolder);
                    }
                }
            } else {
                logger.info("iPhoto folder not found");
            }
        } catch (XmlParseException e) {
            logger.error("Something went wrong with the iPhoto Library scan: ", e);
        } catch (URISyntaxException e) {
            logger.error("Something went wrong with the iPhoto Library scan: ", e);
        } catch (IOException e) {
            logger.error("Something went wrong with the iPhoto Library scan: ", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

    return iPhotoVirtualFolder;
}

From source file:com.bazaarvoice.seo.sdk.BVUIContentServiceProvider.java

private String loadContentFromHttp(URI path) {
    int connectionTimeout = Integer
            .parseInt(_bvConfiguration.getProperty(BVClientConfig.CONNECT_TIMEOUT.getPropertyName()));
    int socketTimeout = Integer
            .parseInt(_bvConfiguration.getProperty(BVClientConfig.SOCKET_TIMEOUT.getPropertyName()));
    String proxyHost = _bvConfiguration.getProperty(BVClientConfig.PROXY_HOST.getPropertyName());
    String charsetConfig = _bvConfiguration.getProperty(BVClientConfig.CHARSET.getPropertyName());
    Charset charset = null;//  w  w w.  j  a v  a 2  s  .  co  m
    try {
        charset = charsetConfig == null ? Charset.defaultCharset() : Charset.forName(charsetConfig);
    } catch (Exception e) {
        _logger.error(BVMessageUtil.getMessage("ERR0024"));
        charset = Charset.defaultCharset();
    }

    String content = null;
    try {
        HttpURLConnection httpUrlConnection = null;
        URL url = path.toURL();

        if (!StringUtils.isBlank(proxyHost) && !"none".equalsIgnoreCase(proxyHost)) {
            int proxyPort = Integer
                    .parseInt(_bvConfiguration.getProperty(BVClientConfig.PROXY_PORT.getPropertyName()));
            SocketAddress socketAddress = new InetSocketAddress(proxyHost, proxyPort);
            Proxy proxy = new Proxy(Type.HTTP, socketAddress);
            httpUrlConnection = (HttpURLConnection) url.openConnection(proxy);
        } else {
            httpUrlConnection = (HttpURLConnection) url.openConnection();
        }

        httpUrlConnection.setConnectTimeout(connectionTimeout);
        httpUrlConnection.setReadTimeout(socketTimeout);

        InputStream is = httpUrlConnection.getInputStream();

        byte[] byteArray = IOUtils.toByteArray(is);
        is.close();

        if (byteArray == null) {
            throw new BVSdkException("ERR0025");
        }

        content = new String(byteArray, charset.name());
    } catch (MalformedURLException e) {
        //           e.printStackTrace();
    } catch (IOException e) {
        //           e.printStackTrace();
        if (e instanceof SocketTimeoutException) {
            throw new BVSdkException(e.getMessage());
        } else {
            throw new BVSdkException("ERR0012");
        }
    } catch (BVSdkException bve) {
        throw bve;
    }

    boolean isValidContent = BVUtilty.validateBVContent(content);
    if (!isValidContent) {
        throw new BVSdkException("ERR0025");
    }

    return content;
}

From source file:crawlercommons.fetcher.SimpleHttpFetcher.java

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

    HttpResponse response;/*  w  w w .j av  a2 s.  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;

    // 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);

    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());
        }

        int httpStatus = response.getStatusLine().getStatusCode();
        if (LOGGER.isTraceEnabled()) {
            fetchTrace.append("; status code: " + httpStatus);
            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 ((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, 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(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) {
            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 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.
                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);
        }
    }

    // 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);
}

From source file:com.mirth.connect.connectors.ws.WebServiceMessageDispatcher.java

/**
 * Returns the URL for the passed in String. If the URL requires
 * authentication, then the WSDL is saved as a temp file and the URL for
 * that file is returned./*from  w  ww  .j a  v  a 2  s  .c  o  m*/
 * 
 * @param wsdlUrl
 * @param username
 * @param password
 * @return
 * @throws Exception
 */
private URL getWsdlUrl(String wsdlUrl, String username, String password) throws Exception {
    URI uri = new URI(wsdlUrl);

    // If the URL points to file, just return it
    if (!uri.getScheme().equalsIgnoreCase("file")) {
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(wsdlUrl);

        int status = client.executeMethod(method);
        if ((status == HttpStatus.SC_UNAUTHORIZED) && (username != null) && (password != null)) {
            client.getState().setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(username, password));
            status = client.executeMethod(method);

            if (status == HttpStatus.SC_OK) {
                String wsdl = method.getResponseBodyAsString();
                File tempFile = File.createTempFile("WebServiceSender", ".wsdl");
                tempFile.deleteOnExit();

                FileUtils.writeStringToFile(tempFile, wsdl);

                return tempFile.toURI().toURL();
            }
        }
    }

    return uri.toURL();
}

From source file:de.sub.goobi.export.download.ExportPdf.java

@Override
public boolean startExport(Process myProcess, URI inZielVerzeichnis) throws ReadException, IOException,
        PreferencesException, TypeNotAllowedForParentException, WriteException {

    /*//from w  w w.  j a  va2  s  .  com
     * Read Document
     */
    Fileformat gdzfile = serviceManager.getProcessService().readMetadataFile(myProcess);
    URI zielVerzeichnis = prepareUserDirectory(inZielVerzeichnis);
    this.myPrefs = serviceManager.getRulesetService().getPreferences(myProcess.getRuleset());

    /*
     * first of all write mets-file in images-Folder of process
     */
    URI metsTempFile = fileService.createResource(myProcess.getTitle() + ".xml");
    writeMetsFile(myProcess, metsTempFile, gdzfile, true);
    Helper.setMeldung(null, myProcess.getTitle() + ": ", "mets file created");
    Helper.setMeldung(null, myProcess.getTitle() + ": ", "start pdf generation now");

    if (logger.isDebugEnabled()) {
        logger.debug("METS file created: " + metsTempFile);
    }

    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getRequest();
    String fullpath = req.getRequestURL().toString();
    String servletpath = context.getExternalContext().getRequestServletPath();
    String myBasisUrl = fullpath.substring(0, fullpath.indexOf(servletpath));

    if (!ConfigCore.getBooleanParameter("pdfAsDownload")) {
        /*
         * use contentserver api for creation of pdf-file
         */
        CreatePdfFromServletThread pdf = new CreatePdfFromServletThread();
        pdf.setMetsURL(metsTempFile.toURL());
        pdf.setTargetFolder(zielVerzeichnis);
        pdf.setInternalServletPath(myBasisUrl);
        if (logger.isDebugEnabled()) {
            logger.debug("Taget directory: " + zielVerzeichnis);
            logger.debug("Using ContentServer2 base URL: " + myBasisUrl);
        }
        pdf.initialize(myProcess);
        pdf.start();
    } else {

        GetMethod method = null;
        try {
            /*
             * define path for mets and pdfs
             */
            URL kitodoContentServerUrl = null;
            String contentServerUrl = ConfigCore.getParameter("kitodoContentServerUrl");
            Integer contentServerTimeOut = ConfigCore.getIntParameter("kitodoContentServerTimeOut", 60000);

            /*
             * using mets file
             */

            if (new MetadatenVerifizierung().validate(myProcess) && metsTempFile.toURL() != null) {
                /*
                 * if no contentserverurl defined use internal
                 * goobiContentServerServlet
                 */
                if (contentServerUrl == null || contentServerUrl.length() == 0) {
                    contentServerUrl = myBasisUrl + "/gcs/gcs?action=pdf&metsFile=";
                }
                kitodoContentServerUrl = new URL(contentServerUrl + metsTempFile.toURL()
                        + AND_TARGET_FILE_NAME_IS + myProcess.getTitle() + PDF_EXTENSION);
                /*
                 * mets data does not exist or is invalid
                 */

            } else {
                if (contentServerUrl == null || contentServerUrl.length() == 0) {
                    contentServerUrl = myBasisUrl + "/cs/cs?action=pdf&images=";
                }
                FilenameFilter filter = new FileNameMatchesFilter("\\d*\\.tif");
                URI imagesDir = serviceManager.getProcessService().getImagesTifDirectory(true, myProcess);
                ArrayList<URI> meta = fileService.getSubUris(filter, imagesDir);
                int capacity = contentServerUrl.length() + (meta.size() - 1) + AND_TARGET_FILE_NAME_IS.length()
                        + myProcess.getTitle().length() + PDF_EXTENSION.length();
                TreeSet<String> filenames = new TreeSet<>(new MetadatenHelper(null, null));
                for (URI data : meta) {
                    String file = data.toURL().toString();
                    filenames.add(file);
                    capacity += file.length();
                }
                StringBuilder url = new StringBuilder(capacity);
                url.append(contentServerUrl);
                boolean subsequent = false;
                for (String f : filenames) {
                    if (subsequent) {
                        url.append('$');
                    } else {
                        subsequent = true;
                    }
                    url.append(f);
                }
                url.append(AND_TARGET_FILE_NAME_IS);
                url.append(myProcess.getTitle());
                url.append(PDF_EXTENSION);
                kitodoContentServerUrl = new URL(url.toString());
            }

            /*
             * get pdf from servlet and forward response to file
             */
            method = new GetMethod(kitodoContentServerUrl.toString());
            method.getParams().setParameter("http.socket.timeout", contentServerTimeOut);

            if (!context.getResponseComplete()) {
                HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
                String fileName = myProcess.getTitle() + PDF_EXTENSION;
                ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
                String contentType = servletContext.getMimeType(fileName);
                response.setContentType(contentType);
                response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
                response.sendRedirect(kitodoContentServerUrl.toString());
                context.responseComplete();
            }
            if (metsTempFile.toURL() != null) {
                File tempMets = new File(metsTempFile.toURL().toString());
                tempMets.delete();
            }
        } catch (Exception e) {

            /*
             * report Error to User as Error-Log
             */
            String text = "error while pdf creation: " + e.getMessage();
            URI uri = zielVerzeichnis.resolve(myProcess.getTitle() + ".PDF-ERROR.log");
            try (BufferedWriter output = new BufferedWriter(new OutputStreamWriter(fileService.write(uri)))) {
                output.write(text);
            } catch (IOException e1) {
                logger.error(e1);
            }
            return false;
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }
    }
    return true;
}

From source file:org.LexGrid.LexBIG.gui.load.LoaderExtensionShell.java

/**
 * Builds the gui.//from w  w w . j a  v a 2s  . c o m
 * 
 * @param shell the shell
 * @param loader the loader
 */
private void buildGUI(final Shell shell, final Loader loader) {

    Group options = new Group(shell, SWT.NONE);
    options.setText("Load Options");
    shell.setLayout(new GridLayout());

    GridData gd = new GridData(GridData.FILL_HORIZONTAL);
    options.setLayoutData(gd);

    GridLayout layout = new GridLayout(1, false);
    options.setLayout(layout);

    Group groupUri = new Group(options, SWT.NONE);
    groupUri.setLayout(new GridLayout(3, false));
    groupUri.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    String uriHelp = "The URI of the resource to load.";

    Label label = new Label(groupUri, SWT.NONE);
    label.setText("URI:");
    label.setToolTipText(uriHelp);

    final Text file = new Text(groupUri, SWT.BORDER);
    file.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
    file.setToolTipText(uriHelp);

    OptionHolder optionHolder = loader.getOptions();

    final Button uriChooseButton;

    if (optionHolder.isResourceUriFolder()) {
        uriChooseButton = Utility.getFolderChooseButton(groupUri, file);
    } else {
        uriChooseButton = Utility.getFileChooseButton(groupUri, file,
                optionHolder.getResourceUriAllowedFileTypes().toArray(new String[0]),
                optionHolder.getResourceUriAllowedFileTypes().toArray(new String[0]));
    }
    uriChooseButton.setToolTipText(uriHelp);

    // Get resolved value sets
    LexEVSResolvedValueSetService resolvedValueSetService = new LexEVSResolvedValueSetServiceImpl();
    java.util.List<CodingScheme> resolvedValueSets = null;
    try {
        resolvedValueSets = resolvedValueSetService.listAllResolvedValueSets();
    } catch (LBException e) {
        resolvedValueSets = null;
    }

    for (final URIOption uriOption : optionHolder.getURIOptions()) {
        Composite group1 = new Composite(options, SWT.NONE);

        group1.setLayout(new GridLayout(3, false));
        group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        Label uriOptionLable = new Label(group1, SWT.NONE);
        uriOptionLable.setText(uriOption.getOptionName() + ":");
        uriOptionLable.setToolTipText(uriOption.getHelpText());

        final Text uriOptionFile = new Text(group1, SWT.BORDER);
        uriOptionFile.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
        uriOptionFile.setToolTipText(uriOption.getHelpText());

        Button uriOptionfileChooseButton = Utility.getFileChooseButton(group1, uriOptionFile,
                uriOption.getAllowedFileExtensions().toArray(new String[0]), null);
        uriOptionfileChooseButton.setToolTipText(uriOption.getHelpText());
        uriOptionfileChooseButton.addSelectionListener(new SelectionListener() {

            public void widgetDefaultSelected(SelectionEvent arg0) {
                //
            }

            public void widgetSelected(SelectionEvent arg0) {
                try {
                    uriOption.setOptionValue(Utility.getAndVerifyURIFromTextField(uriOptionFile));
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    for (final Option<Boolean> boolOption : optionHolder.getBooleanOptions()) {
        Composite group2 = new Composite(options, SWT.NONE);

        RowLayout rlo = new RowLayout();
        rlo.marginWidth = 0;
        group2.setLayout(rlo);

        final Button button = new Button(group2, SWT.CHECK);
        button.setText(boolOption.getOptionName());
        button.setToolTipText(boolOption.getHelpText());

        if (boolOption.getOptionValue() != null) {
            button.setSelection(boolOption.getOptionValue());
        }
        button.addSelectionListener(new SelectionListener() {

            public void widgetDefaultSelected(SelectionEvent event) {
                //
            }

            public void widgetSelected(SelectionEvent event) {
                boolOption.setOptionValue(button.getSelection());
            }

        });
    }

    for (final Option<String> stringOption : optionHolder.getStringOptions()) {
        Composite group3 = new Composite(options, SWT.NONE);

        RowLayout rlo = new RowLayout();
        rlo.marginWidth = 0;
        group3.setLayout(rlo);

        Label textLabel = new Label(group3, SWT.NONE);
        textLabel.setText(stringOption.getOptionName() + ":");
        textLabel.setToolTipText(stringOption.getHelpText());

        if (CollectionUtils.isNotEmpty(stringOption.getPickList())) {
            final Combo comboDropDown = new Combo(group3, SWT.DROP_DOWN | SWT.BORDER);

            comboDropDown.setToolTipText(stringOption.getHelpText());

            for (String pickListItem : stringOption.getPickList()) {

                // Add if it is not a resolved value set
                if (resolvedValueSets != null && !isResolvedValueSet(resolvedValueSets, pickListItem)) {
                    comboDropDown.add(pickListItem);
                }
            }

            comboDropDown.addSelectionListener(new SelectionListener() {

                @Override
                public void widgetDefaultSelected(SelectionEvent arg0) {
                    //
                }

                @Override
                public void widgetSelected(SelectionEvent event) {
                    String option = comboDropDown.getItem(comboDropDown.getSelectionIndex());
                    stringOption.setOptionValue(option);
                }
            });
        } else {

            final Text text = new Text(group3, SWT.BORDER);
            RowData textGd = new RowData();
            textGd.width = 25;
            text.setLayoutData(textGd);

            text.setToolTipText(stringOption.getHelpText());

            text.addModifyListener(new ModifyListener() {

                public void modifyText(ModifyEvent event) {
                    stringOption.setOptionValue(text.getText());
                }
            });
        }
    }

    for (final Option<Integer> integerOption : optionHolder.getIntegerOptions()) {
        Composite group3 = new Composite(options, SWT.NONE);

        RowLayout rlo = new RowLayout();
        rlo.marginWidth = 0;
        group3.setLayout(rlo);

        Label textLabel = new Label(group3, SWT.NONE);
        textLabel.setText(integerOption.getOptionName() + ":");
        textLabel.setToolTipText(integerOption.getHelpText());

        final Text text = new Text(group3, SWT.BORDER);
        text.setToolTipText(integerOption.getHelpText());

        if (integerOption.getOptionValue() != null) {
            text.setText(integerOption.getOptionValue().toString());
        }

        text.addModifyListener(new ModifyListener() {

            public void modifyText(ModifyEvent event) {
                integerOption.setOptionValue(Integer.parseInt(text.getText()));
            }
        });
    }

    for (final MultiValueOption<String> stringArrayOption : optionHolder.getStringArrayOptions()) {
        Composite group4 = new Composite(options, SWT.NONE);

        RowLayout rlo = new RowLayout();
        rlo.marginWidth = 0;
        group4.setLayout(rlo);

        Label textLabel = new Label(group4, SWT.NONE);
        String appendString = CollectionUtils.isNotEmpty(stringArrayOption.getPickList()) ? ""
                : "\n\t(Comma Seperated):";
        textLabel.setText(stringArrayOption.getOptionName() + appendString);
        textLabel.setToolTipText(stringArrayOption.getHelpText());

        if (CollectionUtils.isNotEmpty(stringArrayOption.getPickList())) {
            final List multi = new List(group4, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL);

            multi.setToolTipText(stringArrayOption.getHelpText());

            for (String pickListItem : stringArrayOption.getPickList()) {
                multi.add(pickListItem);
            }
            for (int i = 0; i < stringArrayOption.getPickList().size(); i++) {
                if (stringArrayOption.getOptionValue().contains(stringArrayOption.getPickList().get(i))) {
                    multi.select(i);
                }
            }

            multi.addSelectionListener(new SelectionListener() {

                @Override
                public void widgetDefaultSelected(SelectionEvent arg0) {
                    //
                }

                @Override
                public void widgetSelected(SelectionEvent event) {
                    String[] options = multi.getSelection();
                    stringArrayOption.setOptionValue(Arrays.asList(options));
                }
            });
        } else {
            final Text text = new Text(group4, SWT.BORDER);

            text.setToolTipText(stringArrayOption.getHelpText());

            String arrayString = StringUtils
                    .collectionToCommaDelimitedString(stringArrayOption.getOptionValue());
            text.setText(arrayString);

            text.addModifyListener(new ModifyListener() {

                public void modifyText(ModifyEvent event) {
                    String[] options = StringUtils.commaDelimitedListToStringArray(text.getText());
                    stringArrayOption.setOptionValue(Arrays.asList(options));
                }
            });
        }
    }

    Group groupControlButtons = new Group(options, SWT.NONE);
    groupControlButtons.setLayout(new GridLayout(3, false));
    groupControlButtons.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

    final Button load = new Button(groupControlButtons, SWT.PUSH);
    final Button nextLoad = new Button(groupControlButtons, SWT.PUSH);
    final Button close = new Button(groupControlButtons, SWT.PUSH);
    close.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));

    load.setText("Load");
    load.setToolTipText("Start Load Process.");
    load.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent arg0) {

            URI uri = null;
            // is this a local file?
            File theFile = new File(file.getText());

            if (theFile.exists()) {
                uri = theFile.toURI();
            } else {
                // is it a valid URI (like http://something)
                try {
                    uri = new URI(file.getText());
                    uri.toURL().openConnection();
                } catch (Exception e) {
                    dialog_.showError("Path Error", "No file could be located at this location");
                    return;
                }
            }

            setLoading(true);
            load.setEnabled(false);
            close.setEnabled(false);
            loader.load(uri);

            // Create/start a new thread to update the buttons when the load completes.
            ButtonUpdater buttonUpdater = new ButtonUpdater(nextLoad, close, loader);
            Thread t = new Thread(buttonUpdater);
            t.setDaemon(true);
            t.start();
        }

        public void widgetDefaultSelected(SelectionEvent arg0) {
            // 
        }

    });

    nextLoad.setText("Next Load");
    nextLoad.setToolTipText("Start a New Load Process.");
    nextLoad.setEnabled(false);
    nextLoad.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent arg0) {

            Loader newLoader = null;
            try {
                newLoader = lb_gui_.getLbs().getServiceManager(null).getLoader(loader.getName());
            } catch (LBException e) {
                e.printStackTrace();
            }
            if (!isLoading()) {

                // close the current window and create/initialize it again with the same loader
                shell.dispose();
                setMonitorLoader(true);
                initializeLBGui(newLoader);
            }
        }

        public void widgetDefaultSelected(SelectionEvent arg0) {
            // 
        }

    });

    close.setText("Close");
    close.setToolTipText("Close this Loader Window.");
    close.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent arg0) {
            shell.dispose();
        }

        public void widgetDefaultSelected(SelectionEvent arg0) {
            // 
        }

    });

    Composite status = getStatusComposite(shell, loader);
    gd = new GridData(GridData.FILL_BOTH);
    status.setLayoutData(gd);
}

From source file:gr.iti.mklab.bubing.parser.ITIHTMLParser.java

public void processImageURL(URI pageUri, URI base, String imageUri, String altText)
        throws MalformedURLException, IOException {

    URI url = BURL.parse(imageUri);
    if (url != null) {
        URI resolved = base.resolve(url);
        String resolvedStr = resolved.toString();
        //avoid trying to index the same image multiple times
        if (!ItiAgent.UNIQUE_IMAGE_URLS.mightContain(resolvedStr)) {
            // Put it in the bloom filter even if it is not saved eventually
            // to avoid doing the same checks for the same image a second time
            ItiAgent.UNIQUE_IMAGE_URLS.put(resolvedStr);

            final URLConnection con = resolved.toURL().openConnection();

            if (Utils.checkContentHeaders(con.getContentLength(), con.getContentType())) {

                InputStream is = con.getInputStream();

                BufferedImage image = null;
                try {
                    image = ImageIO.read(is);
                } catch (IllegalArgumentException e) {
                    // this exception is probably thrown because of a greyscale jpeg image
                    System.out.println("Exception: " + e.getMessage() + " | Image: " + imageUri);
                    image = ImageIOGreyScale.read(is); // retry with the modified class
                } catch (MalformedURLException e) {
                    System.out.println("Malformed url exception. Url: " + imageUri);
                }//from   w w  w  . j a  va2s  . c om

                if (Utils.checkImage(image)) {

                    Image item = new Image();
                    item.setUrl(resolvedStr);
                    item.setTitle(altText);
                    item.setWidth(image.getWidth());
                    item.setHeight(image.getHeight());
                    item.setWebPageUrl(pageUri.toString());
                    item.setLastModifiedDate(new Date(con.getLastModified()));
                    item.setObjectId(new ObjectId());

                    try {
                        VisualIndexer.getInstance().indexAndStore(image, item);
                    } catch (Exception e) {
                        System.out.println("HTMLImageParser parse exeption: " + e);
                    }

                }
            }
        }
    }
}

From source file:de.unirostock.sems.cbarchive.web.rest.ShareApi.java

private void addAdditionalFiles(UserManager user, ImportRequest request, Archive archive,
        List<FormDataBodyPart> uploadedFiles) throws ImporterException {

    for (ImportRequest.AdditionalFile addFile : request.getAdditionalFiles()) {
        java.nio.file.Path temp = null;
        try {/*w  w w.j a v a 2s.  co  m*/
            URI remoteUri = new URI(addFile.getRemoteUrl());

            // copy the stream to a temp file
            temp = Files.createTempFile(Fields.TEMP_FILE_PREFIX,
                    FilenameUtils.getBaseName(remoteUri.toString()));
            // write file to disk
            OutputStream output = new FileOutputStream(temp.toFile());
            InputStream input = null;

            String protocol = remoteUri.getScheme().toLowerCase();
            if (protocol.equals("http") || protocol.equals("https")) {
                input = remoteUri.toURL().openStream();
            } else if (protocol.equals("post") && uploadedFiles != null && uploadedFiles.size() > 0) {
                // use a file from the post
                String fileName = remoteUri.getAuthority();
                for (FormDataBodyPart file : uploadedFiles) {
                    if (file.getFormDataContentDisposition().getFileName().equals(fileName)) {
                        input = file.getEntityAs(InputStream.class);
                        break;
                    }
                }
            } else {
                output.close();
                throw new ImporterException(
                        "Unknown protocol " + protocol + " while adding " + remoteUri.toString());
            }

            if (input == null) {
                output.close();
                throw new ImporterException("Cannot open stream to import file: " + remoteUri.toString());
            }

            long downloadedFileSize = IOUtils.copy(input, output);

            output.flush();
            output.close();
            input.close();

            // quota stuff
            // max size for upload
            if (Fields.QUOTA_UPLOAD_SIZE != Fields.QUOTA_UNLIMITED
                    && Tools.checkQuota(downloadedFileSize, Fields.QUOTA_UPLOAD_SIZE) == false) {
                LOGGER.warn("QUOTA_UPLOAD_SIZE reached in workspace ", user.getWorkspaceId());
                throw new ImporterException("The additional file is to big: " + addFile.getRemoteUrl());
            }
            // max archive size
            if (user != null && Fields.QUOTA_ARCHIVE_SIZE != Fields.QUOTA_UNLIMITED
                    && Tools.checkQuota(
                            user.getWorkspace().getArchiveSize(archive.getId()) + downloadedFileSize,
                            Fields.QUOTA_ARCHIVE_SIZE) == false) {
                LOGGER.warn("QUOTA_ARCHIVE_SIZE reached in workspace ", user.getWorkspaceId(),
                        " while trying to adv import archive");
                throw new ImporterException(
                        "The maximum size of the archive is reached, while adding " + addFile.getRemoteUrl());
            }
            // max workspace size
            if (user != null && Fields.QUOTA_WORKSPACE_SIZE != Fields.QUOTA_UNLIMITED && Tools.checkQuota(
                    QuotaManager.getInstance().getWorkspaceSize(user.getWorkspace()) + downloadedFileSize,
                    Fields.QUOTA_WORKSPACE_SIZE) == false) {
                LOGGER.warn("QUOTA_WORKSPACE_SIZE reached in workspace ", user.getWorkspaceId());
                throw new ImporterException(
                        "The maximum size of the workspace is reached, while adding " + addFile.getRemoteUrl());
            }
            // max total size
            if (user != null && Fields.QUOTA_TOTAL_SIZE != Fields.QUOTA_UNLIMITED
                    && Tools.checkQuota(QuotaManager.getInstance().getTotalSize() + downloadedFileSize,
                            Fields.QUOTA_TOTAL_SIZE) == false) {
                LOGGER.warn("QUOTA_TOTAL_SIZE reached in workspace ", user.getWorkspaceId());
                throw new ImporterException(
                        "The maximum size is reached, while adding " + addFile.getRemoteUrl());
            }

            String path = addFile.getArchivePath();
            if (path == null || path.isEmpty())
                path = FilenameUtils.getBaseName(remoteUri.toString());
            // remove leading slash
            if (path.startsWith("/"))
                path = path.substring(1);

            // add it
            ArchiveEntry entry = archive.addArchiveEntry(path, temp, ReplaceStrategy.RENAME);

            // set file format uri
            if (addFile.getFileFormat() != null)
                entry.setFormat(addFile.getFileFormat());

            // add all meta data objects
            if (addFile.getMetaData() != null)
                for (MetaObjectDataholder meta : addFile.getMetaData()) {
                    entry.addDescription(meta.getCombineArchiveMetaObject());
                }

        } catch (URISyntaxException e) {
            LOGGER.error(e, "Wrong defined remoteUrl");
            throw new ImporterException("Cannot parse remote URL: " + addFile.getRemoteUrl(), e);
        } catch (IOException | CombineArchiveWebException e) {
            LOGGER.error(e, "Cannot download an additional file. ", addFile.getRemoteUrl());
            throw new ImporterException("Cannot download and add an additional file: " + addFile.getRemoteUrl(),
                    e);
        } finally {
            if (temp != null && temp.toFile().exists())
                temp.toFile().delete();
        }
    }

}