Example usage for java.net URLConnection getHeaderField

List of usage examples for java.net URLConnection getHeaderField

Introduction

In this page you can find the example usage for java.net URLConnection getHeaderField.

Prototype

public String getHeaderField(int n) 

Source Link

Document

Returns the value for the n th header field.

Usage

From source file:stringreplacer.rewriting.RewriterServlet.java

/**
 * This method in the entry-point for forwarding all HTTP requests made.
 *///from   w w w.j av  a2 s. c o  m
protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {

    /* We code the origin server name as the first directory in the URL
     * being requested. This would normally be setup in an Apache
     * ProxyPass setting. Here we slice out the origin server name from
     * the URL.
     */
    final String origin;
    {
        String pathInfo = request.getPathInfo();
        String[] pathSplit = StringUtils.split(pathInfo, "/");

        if (pathSplit.length == 0) {
            throw new IOException("No origin servername specified on url");
        } else {
            origin = pathSplit[0];
        }
    }

    /* Since the requesting url that was forwarded is placed after the origin
     * server name as a directory, we need to remove the origin server name
     * and get the full URL with query parameters being requested.
     */
    final String path;
    {
        String uri = request.getRequestURI();
        String query = isNotEmpty(request.getQueryString()) ? "?" + request.getQueryString() : "";
        int forwardUriPos = uri.indexOf(origin) + origin.length();
        path = uri.substring(forwardUriPos) + query;
    }

    final URLConnection connection;

    try {
        connection = openUrlConnection(origin, path, request);
        log("Opening: " + connection);
    } catch (FileNotFoundException fnfe) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    /* We now search the content type of all forwarded requests for content
     * types that start with our matching strings because it will be only
     * these content types that we will act upon to rewrite string data.
     */
    boolean matching = false;
    {
        final String originContentType = connection.getContentType().trim().toLowerCase();
        for (String contentType : targetContentTypes) {
            matching = originContentType.startsWith(contentType);
            if (matching) {
                break;
            }
        }
    }

    /* We need to pass all headers that were sent from the origin server
     * to the client, otherwise the client will get a bunch of garbage
     * like raw gzipped output.
     */
    {
        for (String key : connection.getHeaderFields().keySet()) {
            String value = connection.getHeaderField(key);

            /* We have received a HTTP relocation request. We will want to
             * rewrite this url as well. */
            if (key != null && key.trim().equalsIgnoreCase("Location")) {
                log("Redirect: " + value + " => ");
                value = processStringWithRewriters(value);
                log(value);
            }

            response.setHeader(key, value);
        }

        response.setContentType(connection.getContentType());
    }

    /* Use memory to buffer origin request stream otherwise we might experience
     * some hiccups in performance. */
    InputStream in = null;

    try {
        in = new BufferedInputStream(connection.getInputStream());

        // Rewrite the input stream
        if (matching) {
            in = attachNestedStreams(in);
            copyFromOrigin(in, response);

            // Do nothing and just copy it
        } else {
            copyFromOrigin(in, response);
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java

/**
 * Fetches an OAI record for given record identifier.
 * /*from ww w. j  a v a  2s  . c o  m*/
 * @param sourceURL
 * @return itemXML
 * @throws IdentifierNotRecognisedException
 * @throws SourceNotAvailableException
 * @throws RuntimeException
 */
private String fetchOAIRecord(MetadataVO md) throws SourceNotAvailableException, AccessException,
        IdentifierNotRecognisedException, RuntimeException {
    String itemXML = "";
    URLConnection conn;
    Date retryAfter;
    InputStreamReader isReader;
    BufferedReader bReader;
    try {
        conn = ProxyHelper.openConnection(md.getMdUrl());
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        int responseCode = httpConn.getResponseCode();
        switch (responseCode) {
        case 503:
            String retryAfterHeader = conn.getHeaderField("Retry-After");
            if (retryAfterHeader != null) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
                retryAfter = dateFormat.parse(retryAfterHeader);
                this.logger.debug("Source responded with 503, retry after " + retryAfter + ".");
                throw new SourceNotAvailableException(retryAfter);
            } else {
                this.logger.debug(
                        "Source responded with 503, retry after " + this.currentSource.getRetryAfter() + ".");
                throw new SourceNotAvailableException(this.currentSource.getRetryAfter());
            }
        case 302:
            String alternativeLocation = conn.getHeaderField("Location");
            md.setMdUrl(new URL(alternativeLocation));
            this.currentSource = this.sourceHandler.updateMdEntry(this.currentSource, md);
            return fetchOAIRecord(md);
        case 200:
            this.logger.info("Source responded with 200");
            break;
        case 403:
            throw new AccessException("Access to url " + this.currentSource.getName() + " is restricted.");
        default:
            throw new RuntimeException("An error occurred during importing from external system: "
                    + responseCode + ": " + httpConn.getResponseMessage());
        }

        // Get itemXML
        isReader = new InputStreamReader(md.getMdUrl().openStream(), this.enc);
        bReader = new BufferedReader(isReader);
        String line = "";
        while ((line = bReader.readLine()) != null) {
            itemXML += line + "\n";
        }
        httpConn.disconnect();
    } catch (AccessException e) {
        this.logger.error("Access denied.", e);
        throw new AccessException(this.currentSource.getName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return itemXML;
}

From source file:org.tightblog.service.WeblogEntryManager.java

public ValidationResult makeAkismetCall(String apiRequestBody) throws IOException {
    if (!StringUtils.isBlank(akismetApiKey)) {
        URL url = new URL("http://" + akismetApiKey + ".rest.akismet.com/1.1/comment-check");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);//from w ww .  ja  v  a2s.  com

        conn.setRequestProperty("User_Agent", "TightBlog");
        conn.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=utf8");
        conn.setRequestProperty("Content-length", Integer.toString(apiRequestBody.length()));

        OutputStreamWriter osr = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);
        osr.write(apiRequestBody, 0, apiRequestBody.length());
        osr.flush();
        osr.close();

        try (InputStreamReader isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr)) {
            String response = br.readLine();
            if ("true".equals(response)) {
                if ("discard".equalsIgnoreCase(conn.getHeaderField("X-akismet-pro-tip"))) {
                    return ValidationResult.BLATANT_SPAM;
                }
                return ValidationResult.SPAM;
            }
        }
    }

    return ValidationResult.NOT_SPAM;
}

From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java

/**
 * fetch data from a given url.//from ww w .  j a  v a2s.  c  om
 * 
 * @param url
 * @return byte[]
 * @throws SourceNotAvailableException
 * @throws RuntimeException
 * @throws AccessException
 */
public byte[] fetchMetadatafromURL(URL url)
        throws SourceNotAvailableException, RuntimeException, AccessException {
    byte[] input = null;
    URLConnection conn = null;
    Date retryAfter = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);
    try {
        conn = ProxyHelper.openConnection(url);
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        int responseCode = httpConn.getResponseCode();
        switch (responseCode) {
        case 503:
            String retryAfterHeader = conn.getHeaderField("Retry-After");
            if (retryAfterHeader != null) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
                retryAfter = dateFormat.parse(retryAfterHeader);
                this.logger.debug("Source responded with 503, retry after " + retryAfter + ".");
                throw new SourceNotAvailableException(retryAfter);
            }
            break;
        case 302:
            String alternativeLocation = conn.getHeaderField("Location");
            return fetchMetadatafromURL(new URL(alternativeLocation));
        case 200:
            this.logger.info("Source responded with 200.");
            // Fetch file
            GetMethod method = new GetMethod(url.toString());
            HttpClient client = new HttpClient();
            ProxyHelper.executeMethod(client, method);
            input = method.getResponseBody();
            httpConn.disconnect();
            // Create zip file with fetched file
            ZipEntry ze = new ZipEntry("unapi");
            ze.setSize(input.length);
            ze.setTime(this.currentDate());
            CRC32 crc321 = new CRC32();
            crc321.update(input);
            ze.setCrc(crc321.getValue());
            zos.putNextEntry(ze);
            zos.write(input);
            zos.flush();
            zos.closeEntry();
            zos.close();
            this.setContentType("application/zip");
            this.setFileEnding(".zip");
            break;
        case 403:
            throw new AccessException("Access to url " + url + " is restricted.");
        default:
            throw new RuntimeException("An error occurred during importing from external system: "
                    + responseCode + ": " + httpConn.getResponseMessage() + ".");
        }
    } catch (AccessException e) {
        this.logger.error("Access denied.", e);
        throw new AccessException(url.toString());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return baos.toByteArray();
}

From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java

/**
 * Fetches a file via http protocol.//w ww.  jav a  2s .  c  om
 * 
 * @param importSource
 * @param ft
 * @return fetched file as byte[]
 * @throws IdentifierNotRecognisedException
 * @throws RuntimeException
 * @throws AccessException
 */
private byte[] fetchHttpFile(FullTextVO ft)
        throws IdentifierNotRecognisedException, RuntimeException, AccessException {
    URLConnection conn;
    byte[] input = null;

    try {
        conn = ProxyHelper.openConnection(ft.getFtUrl());
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        int responseCode = httpConn.getResponseCode();
        switch (responseCode) {
        case 503:
            // request was not processed by source
            this.logger.warn("Import source " + this.currentSource.getName() + "did not provide file.");
            throw new FormatNotAvailableException(ft.getFtLabel());
        case 302:
            String alternativeLocation = conn.getHeaderField("Location");
            ft.setFtUrl(new URL(alternativeLocation));
            return fetchHttpFile(ft);
        case 200:
            this.logger.info("Source responded with 200.");
            GetMethod method = new GetMethod(ft.getFtUrl().toString());
            HttpClient client = new HttpClient();
            ProxyHelper.executeMethod(client, method);
            input = method.getResponseBody();
            httpConn.disconnect();
            break;
        case 403:
            throw new AccessException("Access to url " + this.currentSource.getName() + " is restricted.");
        default:
            throw new RuntimeException("An error occurred during importing from external system: "
                    + responseCode + ": " + httpConn.getResponseMessage());
        }
    } catch (AccessException e) {
        this.logger.error("Access denied.", e);
        throw new AccessException(this.currentSource.getName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return input;
}

From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java

/**
 * Fetches a record via http protocol.// w  ww  . j  a  v  a2s  .  c o m
 * 
 * @param importSource
 * @param md
 * @return
 * @throws IdentifierNotRecognisedException
 * @throws RuntimeException
 * @throws AccessException
 */
private String fetchHttpRecord(MetadataVO md)
        throws IdentifierNotRecognisedException, RuntimeException, AccessException {
    String item = "";
    URLConnection conn;
    String charset = this.currentSource.getEncoding();
    InputStreamReader isReader;
    BufferedReader bReader;
    try {
        conn = ProxyHelper.openConnection(md.getMdUrl());
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        int responseCode = httpConn.getResponseCode();
        switch (responseCode) {
        case 503:
            // request was not processed by source
            this.logger.warn("Import source " + this.currentSource.getName() + "did not provide file.");
            throw new FormatNotAvailableException(md.getMdLabel());
        case 302:
            String alternativeLocation = conn.getHeaderField("Location");
            md.setMdUrl(new URL(alternativeLocation));
            this.currentSource = this.sourceHandler.updateMdEntry(this.currentSource, md);
            return fetchHttpRecord(md);
        case 200:
            this.logger.info("Source responded with 200");
            break;
        case 403:
            throw new AccessException("Access to url " + this.currentSource.getName() + " is restricted.");
        default:
            throw new RuntimeException("An error occurred during importing from external system: "
                    + responseCode + ": " + httpConn.getResponseMessage());
        }
        // Get itemXML
        isReader = new InputStreamReader(md.getMdUrl().openStream(), charset);
        bReader = new BufferedReader(isReader);
        String line = "";
        while ((line = bReader.readLine()) != null) {
            item += line + "\n";
        }
        httpConn.disconnect();
    } catch (AccessException e) {
        this.logger.error("Access denied.", e);
        throw new AccessException(this.currentSource.getName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return item;
}

From source file:com.spinn3r.api.BaseClient.java

/**
 * Fetch the API with the given FeedConfig
 * //from   w  w  w  . j  av  a  2s .co  m
 * @throws IOException if there's an error with network transport.
 * @throws ParseException if there's a problem parsing the resulting XML.
 */
private PartialBaseClientResult<ResultType> startFetch(Config<ResultType> config, int request_limit)
        throws IOException, InterruptedException {

    PartialBaseClientResult<ResultType> result = new PartialBaseClientResult<ResultType>(config);

    if (config.getVendor() == null)
        throw new RuntimeException("Vendor not specified");

    String resource = config.getNextRequestURL();

    //enforce max limit so that we don't generate runtime exceptions.
    if (request_limit > config.getMaxLimit())
        request_limit = config.getMaxLimit();

    if (resource == null) {

        resource = config.getFirstRequestURL();

        // if the API has NEVER been used before then generate the first
        // request URL from the config parameters.
        if (resource == null)
            resource = config.generateFirstRequestURL(request_limit);

    }

    //apply the request_limit to the current URL.  This needs to be done so
    //that we can change the limit at runtime.  When I originally designed
    //the client I didn't want to support introspecting and mutating the URL
    //on the client but with the optimial limit performance optimization
    //this is impossible.
    resource = setParam(resource, "limit", request_limit);

    // add a connection number to the vendor code
    resource = addConnectionNumber(resource);

    // store the last requested URL so we can expose this to the caller for
    // debug purposes.

    result.setLastRequestURL(resource);
    result.setRequestLimit(request_limit);

    URLConnection conn = getConnection(resource);

    /*
     * If this is an http connection and there is an error code,
     * return throw an error message containing the response 
     * message.
     */
    if (conn instanceof HttpURLConnection) {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        int responseCode = httpConn.getResponseCode();

        if (responseCode >= 400) {
            StringBuilder message = new StringBuilder("");
            InputStream errorStream = httpConn.getErrorStream();
            if (errorStream == null)
                throw new IOException(String.format("Response code %d received", responseCode));

            BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(errorStream)));
            String line;
            while ((line = reader.readLine()) != null)
                message.append(line);

            throw new IOException(message.toString());
        }
    }

    result.setConnection(conn);

    setMoreResults(conn, result);

    result.setNextRequestURL(conn.getHeaderField("X-Next-Request-URL"));

    return result;
}

From source file:de.mpg.mpdl.inge.dataacquisition.DataHandlerBean.java

/**
 * Handlers the http request to fetch a file from an external source.
 * /* w  w w  .  j a va  2 s  .c om*/
 * @param importSource
 * @param fulltext
 * @return byte[] of the fetched file
 * @throws SourceNotAvailableException
 * @throws RuntimeException
 */
private byte[] fetchFile(FullTextVO fulltext)
        throws SourceNotAvailableException, RuntimeException, AccessException, FormatNotAvailableException {
    URLConnection conn = null;
    byte[] input = null;
    try {
        conn = ProxyHelper.openConnection(fulltext.getFtUrl());
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        int responseCode = httpConn.getResponseCode();
        switch (responseCode) {
        case 503:
            // request was not processed by source
            this.logger.warn("Import source " + this.currentSource.getName() + "did not provide data in format "
                    + fulltext.getFtLabel());
            throw new FormatNotAvailableException(fulltext.getFtLabel());
        case 302:
            String alternativeLocation = conn.getHeaderField("Location");
            fulltext.setFtUrl(new URL(alternativeLocation));
            return fetchFile(fulltext);
        case 200:
            this.logger.info("Source responded with 200.");
            GetMethod method = new GetMethod(fulltext.getFtUrl().toString());
            HttpClient client = new HttpClient();
            ProxyHelper.executeMethod(client, method);
            input = method.getResponseBody();
            httpConn.disconnect();
            break;
        case 403:
            throw new AccessException("Access to url " + this.currentSource.getName() + " is restricted.");
        default:
            throw new RuntimeException("An error occurred during importing from external system: "
                    + responseCode + ": " + httpConn.getResponseMessage());
        }
    } catch (AccessException e) {
        this.logger.error("Access denied.", e);
        throw new AccessException(this.currentSource.getName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    // bmc needs transformation from xml to html
    if (this.currentSource.getName().equalsIgnoreCase("BioMed Central")
            && fulltext.getFtFormat().equalsIgnoreCase("text/html")) {
        Format from = new Format("bmc_fulltext_xml", "application/xml", "*");
        Format to = new Format("bmc_fulltext_html", "text/html", "*");
        TransformationBean transformer = new TransformationBean();

        try {
            input = transformer.transform(input, from, to, "escidoc");
        } catch (Exception e) {
            this.logger.error("Could not transform BMC fulltext", e);
        }
    } else if (this.currentSource.getName().equalsIgnoreCase("PubMedCentral")
            && fulltext.getFtFormat().equalsIgnoreCase("application/pdf")) {
        // pmc pdf is generated from oai-pmh-xml
        Format from = new Format("pmc_fulltext_xml", "application/xml", "*");
        Format to = new Format("pmc_fulltext_xslfo", "text/xsl", "*");
        TransformationBean transformer = new TransformationBean();
        byte[] xslFo = null;
        try {
            xslFo = transformer.transform(input, from, to, "escidoc");

        } catch (Exception e) {
            this.logger.error("Could not transform PMC fulltext", e);
        }
        // Step 1: Construct a FopFactory
        FopFactory fopFactory = FopFactory.newInstance();

        // Step 2: Set up output stream.
        ByteArrayOutputStream outputPdf = new ByteArrayOutputStream();

        try {
            // Trying to load FOP-Configuration from the pubman.properties
            fopFactory.setUserConfig(new File(
                    PropertyReader.getProperty("escidoc.dataacquisition.resources.fop.configuration")));
        } catch (Exception e) {
            try {
                logger.info("FopFactory configuration couldn't be loaded from '"
                        + PropertyReader.getProperty("escidoc.dataacquisition.resources.fop.configuration")
                        + "'", e);

                // loading in-EAR configuration an fonts
                String dataaquisitionUrl = DataHandlerBean.class.getClassLoader().getResource("dataaquisition/")
                        .toString();
                logger.info("Trying to load FopFactory from: '" + dataaquisitionUrl + "apache-fop-config.xml'");
                fopFactory.setUserConfig(dataaquisitionUrl + "apache-fop-config.xml");
                fopFactory.setBaseURL(dataaquisitionUrl);
                fopFactory.getFontManager().setFontBaseURL(dataaquisitionUrl + "fonts/");
                if (logger.isDebugEnabled()) {
                    logger.debug(fopFactory.getBaseURL());
                    logger.debug(fopFactory.getFontManager().getFontBaseURL());
                }
            } catch (Exception exception) {
                logger.error("FopFactory configuration wasn't loaded correctly", exception);
            }
        }

        try {
            // Step 3: Construct fop with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outputPdf);

            SortedMap map = (new org.apache.fop.tools.fontlist.FontListGenerator()).listFonts(fopFactory,
                    MimeConstants.MIME_PDF, new org.apache.fop.fonts.FontEventAdapter(
                            new org.apache.fop.events.DefaultEventBroadcaster()));

            // Step 4: Setup JAXP using identity transformer
            TransformerFactory factory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",
                    null);
            Transformer xmlTransformer = factory.newTransformer(); // identity transformer

            // Step 5: Setup input and output for XSLT transformation
            Source src = new StreamSource((InputStream) (new ByteArrayInputStream(xslFo)));

            // Resulting SAX events (the generated FO) must be piped through to FOP
            Result res = new SAXResult(fop.getDefaultHandler());

            // Step 6: Start XSLT transformation and FOP processing
            xmlTransformer.transform(src, res);

            // setting pdf as result
            input = outputPdf.toByteArray();
        } catch (Exception e) {
            logger.error("Error when trying to transform xsl-FO to PDF (Apache-FOP): ", e);
        } finally {
            // Clean-up
            try {
                outputPdf.close();
            } catch (IOException e) {
                logger.error("Couldn't close outputPdf-Stream", e);
            }
        }
    }

    return input;
}

From source file:de.mpg.escidoc.services.dataacquisition.DataHandlerBean.java

/**
 * Handlers the http request to fetch a file from an external source.
 * /*from   ww  w.jav a 2  s.  co m*/
 * @param importSource
 * @param fulltext
 * @return byte[] of the fetched file
 * @throws SourceNotAvailableException
 * @throws RuntimeException
 */
private byte[] fetchFile(FullTextVO fulltext)
        throws SourceNotAvailableException, RuntimeException, AccessException, FormatNotAvailableException {
    URLConnection conn = null;
    byte[] input = null;
    try {
        conn = ProxyHelper.openConnection(fulltext.getFtUrl());
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        int responseCode = httpConn.getResponseCode();
        switch (responseCode) {
        case 503:
            // request was not processed by source
            this.logger.warn("Import source " + this.currentSource.getName() + "did not provide data in format "
                    + fulltext.getFtLabel());
            throw new FormatNotAvailableException(fulltext.getFtLabel());
        case 302:
            String alternativeLocation = conn.getHeaderField("Location");
            fulltext.setFtUrl(new URL(alternativeLocation));
            return fetchFile(fulltext);
        case 200:
            this.logger.info("Source responded with 200.");
            GetMethod method = new GetMethod(fulltext.getFtUrl().toString());
            HttpClient client = new HttpClient();
            ProxyHelper.executeMethod(client, method);
            input = method.getResponseBody();
            httpConn.disconnect();
            break;
        case 403:
            throw new AccessException("Access to url " + this.currentSource.getName() + " is restricted.");
        default:
            throw new RuntimeException("An error occurred during importing from external system: "
                    + responseCode + ": " + httpConn.getResponseMessage());
        }
    } catch (AccessException e) {
        this.logger.error("Access denied.", e);
        throw new AccessException(this.currentSource.getName());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    // bmc needs transformation from xml to html
    if (this.currentSource.getName().equalsIgnoreCase("BioMed Central")
            && fulltext.getFtFormat().equalsIgnoreCase("text/html")) {
        Format from = new Format("bmc_fulltext_xml", "application/xml", "*");
        Format to = new Format("bmc_fulltext_html", "text/html", "*");
        TransformationBean transformer = new TransformationBean();

        try {
            input = transformer.transform(input, from, to, "escidoc");
        } catch (Exception e) {
            this.logger.error("Could not transform BMC fulltext", e);
        }
    } else if (this.currentSource.getName().equalsIgnoreCase("PubMedCentral")
            && fulltext.getFtFormat().equalsIgnoreCase("application/pdf")) {
        // pmc pdf is generated from oai-pmh-xml
        Format from = new Format("pmc_fulltext_xml", "application/xml", "*");
        Format to = new Format("pmc_fulltext_xslfo", "text/xsl", "*");
        TransformationBean transformer = new TransformationBean();
        byte[] xslFo = null;
        try {
            xslFo = transformer.transform(input, from, to, "escidoc");

        } catch (Exception e) {
            this.logger.error("Could not transform PMC fulltext", e);
        }
        // Step 1: Construct a FopFactory
        FopFactory fopFactory = FopFactory.newInstance();

        // Step 2: Set up output stream.
        ByteArrayOutputStream outputPdf = new ByteArrayOutputStream();

        try {
            // Trying to load FOP-Configuration from the pubman.properties
            fopFactory.setUserConfig(new File(
                    PropertyReader.getProperty("escidoc.dataacquisition.resources.fop.configuration")));
        } catch (Exception e) {
            try {
                logger.info("FopFactory configuration couldn't be loaded from '"
                        + PropertyReader.getProperty("escidoc.dataacquisition.resources.fop.configuration")
                        + "'", e);

                // loading in-EAR configuration an fonts
                String dataaquisitionUrl = DataHandlerBean.class.getClassLoader().getResource("dataaquisition/")
                        .toString();
                logger.info("Trying to load FopFactory from: '" + dataaquisitionUrl + "apache-fop-config.xml'");
                fopFactory.setUserConfig(dataaquisitionUrl + "apache-fop-config.xml");
                fopFactory.setBaseURL(dataaquisitionUrl);
                fopFactory.getFontManager().setFontBaseURL(dataaquisitionUrl + "fonts/");
                if (logger.isDebugEnabled()) {
                    logger.debug(fopFactory.getBaseURL());
                    logger.debug(fopFactory.getFontManager().getFontBaseURL());
                }
            } catch (Exception exception) {
                logger.error("FopFactory configuration wasn't loaded correctly", exception);
            }
        }

        try {
            // Step 3: Construct fop with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outputPdf);

            SortedMap map = (new org.apache.fop.tools.fontlist.FontListGenerator()).listFonts(fopFactory,
                    MimeConstants.MIME_PDF, new org.apache.fop.fonts.FontEventAdapter(
                            new org.apache.fop.events.DefaultEventBroadcaster()));

            // Step 4: Setup JAXP using identity transformer
            TransformerFactory factory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",
                    null);
            Transformer xmlTransformer = factory.newTransformer(); // identity transformer

            // Step 5: Setup input and output for XSLT transformation
            Source src = new StreamSource((InputStream) (new ByteArrayInputStream(xslFo)));

            // Resulting SAX events (the generated FO) must be piped through to FOP
            Result res = new SAXResult(fop.getDefaultHandler());

            // Step 6: Start XSLT transformation and FOP processing
            xmlTransformer.transform(src, res);

            // setting pdf as result
            input = outputPdf.toByteArray();
        } catch (Exception e) {
            logger.error("Error when trying to transform xsl-FO to PDF (Apache-FOP): ", e);
        } finally {
            //Clean-up
            try {
                outputPdf.close();
            } catch (IOException e) {
                logger.error("Couldn't close outputPdf-Stream", e);
            }
        }
    }

    return input;
}

From source file:org.eclipse.wst.ws.internal.parser.wsil.WebServicesParser.java

private byte[] getInputStreamAsByteArray(String uriString)
        throws MalformedURLException, IOException, WWWAuthenticationException {
    // Try to get a cached copy of the byte[]
    WebServiceEntity wsEntity = getWebServiceEntityByURI(uriString);
    if (wsEntity != null) {
        byte[] bytes = wsEntity.getBytes();
        if (bytes != null)
            return bytes;
    }//from  ww  w  .j  ava 2 s .  c  o m
    // Get the byte[] by opening a stream to the URI
    URL url = createURL(uriString);
    URLConnection conn = url.openConnection();
    // proxy server setting
    String proxyUserName = System.getProperty("http.proxyUserName");
    String proxyPassword = System.getProperty("http.proxyPassword");
    if (proxyUserName != null && proxyPassword != null) {
        StringBuffer userNamePassword = new StringBuffer(proxyUserName);
        userNamePassword.append(':').append(proxyPassword);
        Base64 encoder = new Base64();
        String encoding = new String(encoder.encode(userNamePassword.toString().getBytes()));
        userNamePassword.setLength(0);
        userNamePassword.append("Basic ").append(encoding);
        conn.setRequestProperty("Proxy-authorization", userNamePassword.toString());
    }
    // HTTP basic authentication setting
    if (httpBasicAuthUsername_ != null && httpBasicAuthPassword_ != null) {
        StringBuffer sb = new StringBuffer(httpBasicAuthUsername_);
        sb.append(':').append(httpBasicAuthPassword_);
        Base64 encoder = new Base64();
        String encoding = new String(encoder.encode(sb.toString().getBytes()));
        sb.setLength(0);
        sb.append("Basic ").append(encoding);
        conn.setRequestProperty("Authorization", sb.toString());
    }
    InputStream is = null;
    try {
        is = conn.getInputStream();
        String wwwAuthMsg = conn.getHeaderField("WWW-Authenticate");
        if (wwwAuthMsg != null)
            throw new WWWAuthenticationException(new IOException(), wwwAuthMsg, uriString);
    } catch (IOException ioe) {
        String wwwAuthMsg = conn.getHeaderField("WWW-Authenticate");
        if (wwwAuthMsg != null)
            throw new WWWAuthenticationException(ioe, wwwAuthMsg, uriString);
        else
            throw ioe;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] b = new byte[1024];
    int byteRead = is.read(b);
    while (byteRead != -1) {
        baos.write(b, 0, byteRead);
        byteRead = is.read(b);
    }
    is.close();
    return baos.toByteArray();
}