Example usage for java.time.format DateTimeFormatter RFC_1123_DATE_TIME

List of usage examples for java.time.format DateTimeFormatter RFC_1123_DATE_TIME

Introduction

In this page you can find the example usage for java.time.format DateTimeFormatter RFC_1123_DATE_TIME.

Prototype

DateTimeFormatter RFC_1123_DATE_TIME

To view the source code for java.time.format DateTimeFormatter RFC_1123_DATE_TIME.

Click Source Link

Document

The RFC-1123 date-time formatter, such as 'Tue, 3 Jun 2008 11:05:30 GMT'.

Usage

From source file:io.github.mikesaelim.arxivoaiharvester.xml.XMLParser.java

/**
 * Parse the date of an article version.
 * @throws ParseException if there is a parsing error
 *//*from w ww  . j  av a 2s .c o m*/
@VisibleForTesting
ZonedDateTime parseSubmissionTime(String value) {
    ZonedDateTime submissionTime;
    try {
        submissionTime = ZonedDateTime.parse(value, DateTimeFormatter.RFC_1123_DATE_TIME);
    } catch (DateTimeParseException e) {
        throw new ParseException("Could not parse version date '" + value + "' in RFC_1123_DATE_TIME format",
                e);
    }

    return submissionTime;
}

From source file:org.ops4j.pax.web.itest.undertow.WarJsfResourcehandlerIntegrationTest.java

/**
 * Does multiple assertions in one test since container-startup is slow
 * <p>//from w w w.j a  v a  2  s  . c  om
 * <pre>
 * <ul>
 *    <li>Check if pax-web-resources-jsf is started</li>
 *    <li>Check if application under test (jsf-application-myfaces) is started
 *    <li>Test actual resource-handler
 *       <ul>
 *          <li>Test for occurence of 'Hello JSF' (jsf-application-myfaces)</li>
 *          <li>Test for occurence of 'Standard Header' (jsf-resourcebundle)</li>
 *          <li>Test for occurence of 'iceland.jpg' from library 'default' in version '2_0' (jsf-resourcebundle)</li>
 *          <li>Test for occurence of 'Customized Footer' (jsf-resourcebundle)</li>
 *          <li>Access a resource (image) via HTTP which gets loaded from a other bundle (jsf-resourcebundle)</li>
 *       </ul>
 *    </li>
 *  <li>Test localized resource
 *        <ul>
 *          <li>Test for occurence of 'flag.png' from library 'layout' with default locale 'en' which resolves to 'iceland' (default in faces-config)</li>
 *            <li>Test for occurence of 'flag.png' from library 'layout' with default locale 'de' which resolves to 'germany'</li>
 *        </ul>
 *    </li>
 *    <li>Test resource-overide
 *        <ul>
 *            <li>Install another bundle (jsf-resourcebundle-override) which also serves  template/footer.xhtml</li>
 *            <li>Test for occurence of 'Overriden Footer' (jsf-resourcebundle-override)</li>
 *          <li>Test for occurence of 'iceland.jpg' from library 'default' in version '3_0' (jsf-resourcebundle-override)</li>
 *            <li>Uninstall the previously installed bundle</li>
 *            <li>Test again, this time for occurence of 'Customized Footer' (jsf-resourcebundle)</li>
 *        </ul>
 *    </li>
 *    <li>
 *        Test {@link OsgiResource#userAgentNeedsUpdate(FacesContext)}
 *        with an If-Modified-Since header
 *    </li>
 *    <li>Test servletmapping with prefix (faces/*) rather than extension for both, page and image serving</li>
 * </ul>
 * </pre>
 */
@Test
public void testJsfResourceHandler() throws Exception {
    final String pageUrl = "http://127.0.0.1:8181/osgi-resourcehandler-myfaces/index.xhtml";
    final String imageUrl = "http://127.0.0.1:8181/osgi-resourcehandler-myfaces/javax.faces.resource/images/iceland.jpg.xhtml?type=osgi&ln=default&lv=2_0";

    // prepare Bundle
    initWebListener();
    installAndStartBundle(mavenBundle().groupId("org.ops4j.pax.web.samples")
            .artifactId("jsf-resourcehandler-myfaces").versionAsInProject().getURL());

    waitForWebListener();
    new WaitCondition2("pax-web-resources-extender done scanning for webresources-bundles", () -> {
        try {
            HttpTestClientFactory.createDefaultTestClient().doGETandExecuteTest(imageUrl);
            return true;
        } catch (AssertionError | Exception e) {
            return false;
        }
    }).waitForCondition(20000, 1000,
            () -> fail("Image not served in time. pax-web-resources-extender not finished"));

    // start testing
    BundleMatchers.isBundleActive("org.ops4j.pax.web.pax-web-resources-extender", bundleContext);
    BundleMatchers.isBundleActive("org.ops4j.pax.web.pax-web-resources-jsf", bundleContext);
    BundleMatchers.isBundleActive("jsf-resourcehandler-resourcebundle", bundleContext);
    BundleMatchers.isBundleActive("jsf-resourcehandler-myfaces", bundleContext);

    HttpTestClientFactory.createDefaultTestClient().withResponseAssertion(
            "Some Content shall be included from the jsf-application-bundle to test internal view-resources",
            resp -> StringUtils.contains(resp, "Hello Included Content"))
            .withResponseAssertion(
                    "Standard header shall be loaded from resourcebundle to test external view-resources",
                    resp -> StringUtils.contains(resp, "Standard Header"))
            .withResponseAssertion("Images shall be loaded from resourcebundle to test external resources",
                    resp -> StringUtils.contains(resp, "iceland.jpg"))
            .withResponseAssertion(
                    "Customized footer shall be loaded from resourcebundle to test external view-resources",
                    resp -> StringUtils.contains(resp, "Customized Footer"))
            .withResponseAssertion("Image-URL must be created from OsgiResource", resp -> StringUtils.contains(
                    resp,
                    "/osgi-resourcehandler-myfaces/javax.faces.resource/images/iceland.jpg.xhtml?type=osgi&amp;ln=default&amp;lv=2_0"))
            .withResponseAssertion("Flag-URL must be served from iceland-folder", resp -> StringUtils.contains(
                    resp,
                    "/osgi-resourcehandler-myfaces/javax.faces.resource/flag.png.xhtml?type=osgi&amp;loc=iceland&amp;ln=layout"))
            .doGETandExecuteTest(pageUrl);
    // Test German image
    HttpTestClientFactory.createDefaultTestClient()
            // set header for german-locale in JSF
            .addRequestHeader("Accept-Language", "de").withReturnCode(200)
            .withResponseAssertion("Flag-URL must be served from germany-folder", resp -> StringUtils.contains(
                    resp,
                    "/osgi-resourcehandler-myfaces/javax.faces.resource/flag.png.xhtml?type=osgi&amp;loc=germany&amp;ln=layout"))
            .doGETandExecuteTest(pageUrl);
    // test resource serving for image
    HttpTestClientFactory.createDefaultTestClient().doGETandExecuteTest(imageUrl);
    // Install override bundle
    String bundlePath = mavenBundle().groupId("org.ops4j.pax.web.samples")
            .artifactId("jsf-resourcehandler-resourcebundle-override").versionAsInProject().getURL();
    Bundle installedResourceBundle = installAndStartBundle(bundlePath);
    BundleMatchers.isBundleActive(installedResourceBundle.getSymbolicName(), bundleContext);

    HttpTestClientFactory.createDefaultTestClient().withResponseAssertion(
            "Overriden footer shall be loaded from resourcebundle-override  to test external view-resources which are overriden",
            resp -> StringUtils.contains(resp, "Overriden Footer"))
            .withResponseAssertion("Iceland-Picture shall be found in version 3.0 from resourcebunde-override",
                    resp -> StringUtils.contains(resp,
                            "javax.faces.resource/images/iceland.jpg.xhtml?type=osgi&amp;ln=default&amp;lv=2_0&amp;rv=3_0.jpg"))
            .doGETandExecuteTest(pageUrl);

    // uninstall overriding bundle
    installedResourceBundle.stop();

    new WaitCondition2("Customized footer shall be loaded from resourcebundle", () -> {
        try {
            HttpTestClientFactory.createDefaultTestClient()
                    .withResponseAssertion("Customized footer shall be loaded from resourcebundle",
                            resp -> StringUtils.contains(resp, "Customized Footer"))
                    .doGETandExecuteTest(pageUrl);
            return true;
        } catch (AssertionError | Exception e) {
            return false;
        }
    }).waitForCondition(5000, 1000,
            () -> fail("After uninstalling 'jsf-resourcehandler-resourcebundle-override' "
                    + "the customized foot must be loaded again."));

    // Test If-Modified-Since
    ZonedDateTime now = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of(ZoneId.SHORT_IDS.get("ECT")));
    // "Modified-Since should mark response with 304"
    HttpTestClientFactory.createDefaultTestClient().withReturnCode(304)
            .addRequestHeader("If-Modified-Since", now.format(DateTimeFormatter.RFC_1123_DATE_TIME))
            .doGETandExecuteTest(imageUrl);

    // Test second faces-mapping which uses a prefix (faces/*)
    final String pageUrlWithPrefixMapping = "http://127.0.0.1:8181/osgi-resourcehandler-myfaces/faces/index.xhtml";
    final String imageUrlWithPrefixMapping = "http://127.0.0.1:8181/osgi-resourcehandler-myfaces/faces/javax.faces.resource/images/iceland.jpg?type=osgi&ln=default&lv=2_0";

    HttpTestClientFactory.createDefaultTestClient().doGETandExecuteTest(imageUrlWithPrefixMapping);
    HttpTestClientFactory.createDefaultTestClient().withResponseAssertion(
            "Image-URL must be created from OsgiResource. This time the second servlet-mapping (faces/*) must be used.",
            resp -> StringUtils.contains(resp,
                    "/osgi-resourcehandler-myfaces/faces/javax.faces.resource/images/iceland.jpg?type=osgi&amp;ln=default&amp;lv=2_0"))
            .doGETandExecuteTest(pageUrlWithPrefixMapping);
}

From source file:org.ops4j.pax.web.itest.jetty.WarJsfResourcehandlerIntegrationTest.java

/**
 * Does multiple assertions in one test since container-startup is slow
 * <p>/*w  w w.  j  a va 2s. c o m*/
 * <pre>
 * <ul>
 *    <li>Check if pax-web-resources-jsf is started</li>
 *    <li>Check if application under test (jsf-application-myfaces) is started
 *    <li>Test actual resource-handler
 *       <ul>
 *          <li>Test for occurence of 'Hello JSF' (jsf-application-myfaces)</li>
 *          <li>Test for occurence of 'Standard Header' (jsf-resourcebundle)</li>
 *          <li>Test for occurence of 'iceland.jpg' from library 'default' in version '2_0' (jsf-resourcebundle)</li>
 *          <li>Test for occurence of 'Customized Footer' (jsf-resourcebundle)</li>
 *          <li>Access a resource (image) via HTTP which gets loaded from a other bundle (jsf-resourcebundle)</li>
 *       </ul>
 *    </li>
 *  <li>Test localized resource
 *        <ul>
 *          <li>Test for occurence of 'flag.png' from library 'layout' with default locale 'en' which resolves to 'iceland' (default in faces-config)</li>
 *            <li>Test for occurence of 'flag.png' from library 'layout' with default locale 'de' which resolves to 'germany'</li>
 *        </ul>
 *    </li>
 *    <li>Test resource-overide
 *        <ul>
 *            <li>Install another bundle (jsf-resourcebundle-override) which also serves  template/footer.xhtml</li>
 *            <li>Test for occurence of 'Overriden Footer' (jsf-resourcebundle-override)</li>
 *          <li>Test for occurence of 'iceland.jpg' from library 'default' in version '3_0' (jsf-resourcebundle-override)</li>
 *            <li>Uninstall the previously installed bundle</li>
 *            <li>Test again, this time for occurence of 'Customized Footer' (jsf-resourcebundle)</li>
 *        </ul>
 *    </li>
 *    <li>
 *        Test {@link OsgiResource#userAgentNeedsUpdate(FacesContext)}
 *        with an If-Modified-Since header
 *    </li>
 *    <li>Test servletmapping with prefix (faces/*) rather than extension for both, page and image serving</li>
 * </ul>
 * </pre>
 */
@Test
public void testJsfResourceHandler() throws Exception {
    final String pageUrl = "http://127.0.0.1:8181/osgi-resourcehandler-myfaces/index.xhtml";
    final String imageUrl = "http://127.0.0.1:8181/osgi-resourcehandler-myfaces/javax.faces.resource/images/iceland.jpg.xhtml?type=osgi&ln=default&lv=2_0";

    // prepare Bundle
    initWebListener();
    installAndStartBundle(mavenBundle().groupId("org.ops4j.pax.web.samples")
            .artifactId("jsf-resourcehandler-myfaces").versionAsInProject().getURL());

    waitForWebListener();

    new WaitCondition2("pax-web-resources-extender done scanning for webresources-bundles", () -> {
        try {
            HttpTestClientFactory.createDefaultTestClient().doGETandExecuteTest(imageUrl);
            return true;
        } catch (AssertionError | Exception e) {
            return false;
        }
    }).waitForCondition(20000, 1000,
            () -> fail("Image not served in time. pax-web-resources-extender not finished"));

    // start testing
    BundleMatchers.isBundleActive("org.ops4j.pax.web.pax-web-resources-extender", bundleContext);
    BundleMatchers.isBundleActive("org.ops4j.pax.web.pax-web-resources-jsf", bundleContext);
    BundleMatchers.isBundleActive("jsf-resourcehandler-resourcebundle", bundleContext);
    BundleMatchers.isBundleActive("jsf-resourcehandler-myfaces", bundleContext);

    HttpTestClientFactory.createDefaultTestClient().withResponseAssertion(
            "Some Content shall be included from the jsf-application-bundle to test internal view-resources",
            resp -> StringUtils.contains(resp, "Hello Included Content"))
            .withResponseAssertion(
                    "Standard header shall be loaded from resourcebundle to test external view-resources",
                    resp -> StringUtils.contains(resp, "Standard Header"))
            .withResponseAssertion("Images shall be loaded from resourcebundle to test external resources",
                    resp -> StringUtils.contains(resp, "iceland.jpg"))
            .withResponseAssertion(
                    "Customized footer shall be loaded from resourcebundle to test external view-resources",
                    resp -> StringUtils.contains(resp, "Customized Footer"))
            .withResponseAssertion("Image-URL must be created from OsgiResource", resp -> StringUtils.contains(
                    resp,
                    "/osgi-resourcehandler-myfaces/javax.faces.resource/images/iceland.jpg.xhtml?type=osgi&amp;ln=default&amp;lv=2_0"))
            .withResponseAssertion("Flag-URL must be served from iceland-folder", resp -> StringUtils.contains(
                    resp,
                    "/osgi-resourcehandler-myfaces/javax.faces.resource/flag.png.xhtml?type=osgi&amp;loc=iceland&amp;ln=layout"))
            .doGETandExecuteTest(pageUrl);
    // Test German image
    HttpTestClientFactory.createDefaultTestClient()
            // set header for german-locale in JSF
            .addRequestHeader("Accept-Language", "de").withReturnCode(200)
            .withResponseAssertion("Flag-URL must be served from germany-folder", resp -> StringUtils.contains(
                    resp,
                    "/osgi-resourcehandler-myfaces/javax.faces.resource/flag.png.xhtml?type=osgi&amp;loc=germany&amp;ln=layout"))
            .doGETandExecuteTest(pageUrl);
    // test resource serving for image
    HttpTestClientFactory.createDefaultTestClient().doGETandExecuteTest(imageUrl);
    // Install override bundle
    String bundlePath = mavenBundle().groupId("org.ops4j.pax.web.samples")
            .artifactId("jsf-resourcehandler-resourcebundle-override").versionAsInProject().getURL();
    Bundle installedResourceBundle = installAndStartBundle(bundlePath);
    BundleMatchers.isBundleActive(installedResourceBundle.getSymbolicName(), bundleContext);

    HttpTestClientFactory.createDefaultTestClient().withResponseAssertion(
            "Overriden footer shall be loaded from resourcebundle-override  to test external view-resources which are overriden",
            resp -> StringUtils.contains(resp, "Overriden Footer"))
            .withResponseAssertion("Iceland-Picture shall be found in version 3.0 from resourcebunde-override",
                    resp -> StringUtils.contains(resp,
                            "javax.faces.resource/images/iceland.jpg.xhtml?type=osgi&amp;ln=default&amp;lv=2_0&amp;rv=3_0.jpg"))
            .doGETandExecuteTest(pageUrl);

    // uninstall overriding bundle
    installedResourceBundle.stop();

    new WaitCondition2("Customized footer shall be loaded from resourcebundle", () -> {
        try {
            HttpTestClientFactory.createDefaultTestClient()
                    .withResponseAssertion("Customized footer shall be loaded from resourcebundle",
                            resp -> StringUtils.contains(resp, "Customized Footer"))
                    .doGETandExecuteTest(pageUrl);
            return true;
        } catch (AssertionError | Exception e) {
            return false;
        }
    }).waitForCondition(5000, 1000,
            () -> fail("After uninstalling 'jsf-resourcehandler-resourcebundle-override' "
                    + "the customized foot must be loaded again."));

    // Test If-Modified-Since
    ZonedDateTime now = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of(ZoneId.SHORT_IDS.get("ECT")));
    // "Modified-Since should mark response with 304"
    HttpTestClientFactory.createDefaultTestClient().withReturnCode(304)
            .addRequestHeader("If-Modified-Since", now.format(DateTimeFormatter.RFC_1123_DATE_TIME))
            .doGETandExecuteTest(imageUrl);

    // Test second faces-mapping which uses a prefix (faces/*)
    final String pageUrlWithPrefixMapping = "http://127.0.0.1:8181/osgi-resourcehandler-myfaces/faces/index.xhtml";
    final String imageUrlWithPrefixMapping = "http://127.0.0.1:8181/osgi-resourcehandler-myfaces/faces/javax.faces.resource/images/iceland.jpg?type=osgi&ln=default&lv=2_0";

    HttpTestClientFactory.createDefaultTestClient().doGETandExecuteTest(imageUrlWithPrefixMapping);
    HttpTestClientFactory.createDefaultTestClient().withResponseAssertion(
            "Image-URL must be created from OsgiResource. This time the second servlet-mapping (faces/*) must be used.",
            resp -> StringUtils.contains(resp,
                    "/osgi-resourcehandler-myfaces/faces/javax.faces.resource/images/iceland.jpg?type=osgi&amp;ln=default&amp;lv=2_0"))
            .doGETandExecuteTest(pageUrlWithPrefixMapping);
}

From source file:org.talend.dataquality.statistics.datetime.utils.PatternListGenerator.java

@SuppressWarnings("unused")
private static void validateISOPattens(List<String> isoPatternList) {

    Set<String> formattedDateTimeSet = new HashSet<String>();
    for (String pattern : isoPatternList) {
        formattedDateTimeSet.add(getFormattedDateTime(pattern, Locale.US));
    }/*  w w  w .  j  a v a  2s.c om*/

    DateTimeFormatter[] formatters = new DateTimeFormatter[] { DateTimeFormatter.BASIC_ISO_DATE, // 1
            DateTimeFormatter.ISO_DATE, // 2
            DateTimeFormatter.ISO_DATE_TIME, // 3
            // DateTimeFormatter.ISO_TIME, //
            DateTimeFormatter.ISO_INSTANT, // 4
            DateTimeFormatter.ISO_LOCAL_DATE, // 5
            DateTimeFormatter.ISO_LOCAL_DATE_TIME, // 6
            // DateTimeFormatter.ISO_LOCAL_TIME, //
            DateTimeFormatter.ISO_OFFSET_DATE, // 7
            DateTimeFormatter.ISO_OFFSET_DATE_TIME, // 8
            // DateTimeFormatter.ISO_OFFSET_TIME, //
            DateTimeFormatter.ISO_ORDINAL_DATE, // 9
            DateTimeFormatter.ISO_WEEK_DATE, // 10
            DateTimeFormatter.ISO_ZONED_DATE_TIME, // 11
            DateTimeFormatter.RFC_1123_DATE_TIME, // 12
    };

    System.out.println("-------------Validate ISO PattenText-------------");
    for (int i = 0; i < formatters.length; i++) {

        System.out.print((i + 1) + "\t");
        try {
            String formattedDateTime = ZONED_DATE_TIME.format(formatters[i]);
            System.out.print(formattedDateTimeSet.contains(formattedDateTime) ? "YES\t" : "NO\t");
            System.out.println(formattedDateTime);
        } catch (Throwable t) {
            System.out.println(t.getMessage());
        }
    }

}

From source file:org.ops4j.pax.web.itest.tomcat.WarJsfResourcehandlerIntegrationTest.java

/**
 * Does multiple assertions in one test since container-startup is slow
 * <p>//  w w w .j  av  a  2  s  .  c  o m
 * <pre>
 * <ul>
 *    <li>Check if pax-web-resources-jsf is started</li>
 *    <li>Check if application under test (jsf-application-myfaces) is started
 *    <li>Test actual resource-handler
 *       <ul>
 *          <li>Test for occurence of 'Hello JSF' (jsf-application-myfaces)</li>
 *          <li>Test for occurence of 'Standard Header' (jsf-resourcebundle)</li>
 *          <li>Test for occurence of 'iceland.jpg' from library 'default' in version '2_0' (jsf-resourcebundle)</li>
 *          <li>Test for occurence of 'Customized Footer' (jsf-resourcebundle)</li>
 *          <li>Access a resource (image) via HTTP which gets loaded from a other bundle (jsf-resourcebundle)</li>
 *       </ul>
 *    </li>
 *  <li>Test localized resource
 *        <ul>
 *          <li>Test for occurence of 'flag.png' from library 'layout' with default locale 'en' which resolves to 'iceland' (default in faces-config)</li>
 *            <li>Test for occurence of 'flag.png' from library 'layout' with default locale 'de' which resolves to 'germany'</li>
 *        </ul>
 *    </li>
 *    <li>Test resource-overide
 *        <ul>
 *            <li>Install another bundle (jsf-resourcebundle-override) which also serves  template/footer.xhtml</li>
 *            <li>Test for occurence of 'Overriden Footer' (jsf-resourcebundle-override)</li>
 *          <li>Test for occurence of 'iceland.jpg' from library 'default' in version '3_0' (jsf-resourcebundle-override)</li>
 *            <li>Uninstall the previously installed bundle</li>
 *            <li>Test again, this time for occurence of 'Customized Footer' (jsf-resourcebundle)</li>
 *        </ul>
 *    </li>
 *    <li>
 *        Test {@link OsgiResource#userAgentNeedsUpdate(FacesContext)}
 *        with an If-Modified-Since header
 *    </li>
 *    <li>Test servletmapping with prefix (faces/*) rather than extension for both, page and image serving</li>
 * </ul>
 * </pre>
 */
@Test
public void testJsfResourceHandler() throws Exception {
    final String pageUrl = "http://127.0.0.1:8282/osgi-resourcehandler-myfaces/index.xhtml";
    final String imageUrl = "http://127.0.0.1:8282/osgi-resourcehandler-myfaces/javax.faces.resource/images/iceland.jpg.xhtml?type=osgi&ln=default&lv=2_0";

    // prepare Bundle
    initWebListener();
    installAndStartBundle(mavenBundle().groupId("org.ops4j.pax.web.samples")
            .artifactId("jsf-resourcehandler-myfaces").versionAsInProject().getURL());

    waitForWebListener();
    new WaitCondition2("pax-web-resources-extender done scanning for webresources-bundles", () -> {
        try {
            HttpTestClientFactory.createDefaultTestClient().doGETandExecuteTest(imageUrl);
            return true;
        } catch (AssertionError | Exception e) {
            return false;
        }
    }).waitForCondition(20000, 1000,
            () -> fail("Image not served in time. pax-web-resources-extender not finished"));

    // start testing
    BundleMatchers.isBundleActive("org.ops4j.pax.web.pax-web-resources-extender", bundleContext);
    BundleMatchers.isBundleActive("org.ops4j.pax.web.pax-web-resources-jsf", bundleContext);
    BundleMatchers.isBundleActive("jsf-resourcehandler-resourcebundle", bundleContext);
    BundleMatchers.isBundleActive("jsf-resourcehandler-myfaces", bundleContext);

    HttpTestClientFactory.createDefaultTestClient().withResponseAssertion(
            "Some Content shall be included from the jsf-application-bundle to test internal view-resources",
            resp -> StringUtils.contains(resp, "Hello Included Content"))
            .withResponseAssertion(
                    "Standard header shall be loaded from resourcebundle to test external view-resources",
                    resp -> StringUtils.contains(resp, "Standard Header"))
            .withResponseAssertion("Images shall be loaded from resourcebundle to test external resources",
                    resp -> StringUtils.contains(resp, "iceland.jpg"))
            .withResponseAssertion(
                    "Customized footer shall be loaded from resourcebundle to test external view-resources",
                    resp -> StringUtils.contains(resp, "Customized Footer"))
            .withResponseAssertion("Image-URL must be created from OsgiResource", resp -> StringUtils.contains(
                    resp,
                    "/osgi-resourcehandler-myfaces/javax.faces.resource/images/iceland.jpg.xhtml?type=osgi&amp;ln=default&amp;lv=2_0"))
            .withResponseAssertion("Flag-URL must be served from iceland-folder", resp -> StringUtils.contains(
                    resp,
                    "/osgi-resourcehandler-myfaces/javax.faces.resource/flag.png.xhtml?type=osgi&amp;loc=iceland&amp;ln=layout"))
            .doGETandExecuteTest(pageUrl);
    // Test German image
    HttpTestClientFactory.createDefaultTestClient()
            // set header for german-locale in JSF
            .addRequestHeader("Accept-Language", "de").withReturnCode(200)
            .withResponseAssertion("Flag-URL must be served from germany-folder", resp -> StringUtils.contains(
                    resp,
                    "/osgi-resourcehandler-myfaces/javax.faces.resource/flag.png.xhtml?type=osgi&amp;loc=germany&amp;ln=layout"))
            .doGETandExecuteTest(pageUrl);
    // test resource serving for image
    HttpTestClientFactory.createDefaultTestClient().doGETandExecuteTest(imageUrl);
    // Install override bundle
    String bundlePath = mavenBundle().groupId("org.ops4j.pax.web.samples")
            .artifactId("jsf-resourcehandler-resourcebundle-override").versionAsInProject().getURL();
    Bundle installedResourceBundle = installAndStartBundle(bundlePath);
    BundleMatchers.isBundleActive(installedResourceBundle.getSymbolicName(), bundleContext);

    HttpTestClientFactory.createDefaultTestClient().withResponseAssertion(
            "Overriden footer shall be loaded from resourcebundle-override  to test external view-resources which are overriden",
            resp -> StringUtils.contains(resp, "Overriden Footer"))
            .withResponseAssertion("Iceland-Picture shall be found in version 3.0 from resourcebunde-override",
                    resp -> StringUtils.contains(resp,
                            "javax.faces.resource/images/iceland.jpg.xhtml?type=osgi&amp;ln=default&amp;lv=2_0&amp;rv=3_0.jpg"))
            .doGETandExecuteTest(pageUrl);

    // uninstall overriding bundle
    installedResourceBundle.stop();

    new WaitCondition2("Customized footer shall be loaded from resourcebundle", () -> {
        try {
            HttpTestClientFactory.createDefaultTestClient()
                    .withResponseAssertion("Customized footer shall be loaded from resourcebundle",
                            resp -> StringUtils.contains(resp, "Customized Footer"))
                    .doGETandExecuteTest(pageUrl);
            return true;
        } catch (AssertionError | Exception e) {
            return false;
        }
    }).waitForCondition(5000, 1000,
            () -> fail("After uninstalling 'jsf-resourcehandler-resourcebundle-override' "
                    + "the customized foot must be loaded again."));

    // Test If-Modified-Since
    ZonedDateTime now = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of(ZoneId.SHORT_IDS.get("ECT")));
    // "Modified-Since should mark response with 304"
    HttpTestClientFactory.createDefaultTestClient().withReturnCode(304)
            .addRequestHeader("If-Modified-Since", now.format(DateTimeFormatter.RFC_1123_DATE_TIME))
            .doGETandExecuteTest(imageUrl);

    // Test second faces-mapping which uses a prefix (faces/*)
    final String pageUrlWithPrefixMapping = "http://127.0.0.1:8282/osgi-resourcehandler-myfaces/faces/index.xhtml";
    final String imageUrlWithPrefixMapping = "http://127.0.0.1:8282/osgi-resourcehandler-myfaces/faces/javax.faces.resource/images/iceland.jpg?type=osgi&ln=default&lv=2_0";

    HttpTestClientFactory.createDefaultTestClient().doGETandExecuteTest(imageUrlWithPrefixMapping);
    HttpTestClientFactory.createDefaultTestClient().withResponseAssertion(
            "Image-URL must be created from OsgiResource. This time the second servlet-mapping (faces/*) must be used.",
            resp -> StringUtils.contains(resp,
                    "/osgi-resourcehandler-myfaces/faces/javax.faces.resource/images/iceland.jpg?type=osgi&amp;ln=default&amp;lv=2_0"))
            .doGETandExecuteTest(pageUrlWithPrefixMapping);
}

From source file:net.www_eee.portal.channels.ProxyChannel.java

@Override
protected void doViewRequestImpl(final Page.Request pageRequest, final ViewResponse viewResponse)
        throws WWWEEEPortal.Exception, WebApplicationException {
    final HttpClientContext proxyContext = doProxyRequest(pageRequest, Mode.VIEW);

    final @NonNull HttpResponse proxyResponse = proxyContext.getResponse();
    final URL proxiedFileURL = HttpUtil.getRequestTargetURL(proxyContext);

    try (final CloseableHttpClient proxyClient = Objects
            .requireNonNull((CloseableHttpClient) proxyContext.getAttribute(HTTP_CLIENT_CONTEXT_ID))) {

        final MimeType responseContentType = getProxyResponseHeader(pageRequest, proxyResponse, "Content-Type",
                IOUtil::newMimeType);// w w w. jav  a 2 s  .c  om
        viewResponse.setContentType(
                (responseContentType != null) ? RESTUtil.getMediaType(responseContentType) : null);

        viewResponse.setLastModified(getProxyResponseHeader(pageRequest, proxyResponse, "Last-Modified",
                (s) -> ZonedDateTime.parse(s, DateTimeFormatter.RFC_1123_DATE_TIME).toInstant()));
        viewResponse.setCacheControl(mergeCacheControl(viewResponse.getCacheControl(),
                getProxyResponseHeader(pageRequest, proxyResponse, "Cache-Control", CacheControl::valueOf)));
        viewResponse.setExpires(getProxyResponseHeader(pageRequest, proxyResponse, "Expires",
                (s) -> ZonedDateTime.parse(s, DateTimeFormatter.RFC_1123_DATE_TIME).toInstant()));
        viewResponse
                .setEntityTag(getProxyResponseHeader(pageRequest, proxyResponse, "ETag", EntityTag::valueOf));
        viewResponse.setLocale(getProxyResponseHeader(pageRequest, proxyResponse, "Content-Language",
                (h) -> Locale.forLanguageTag(
                        CollUtil.first(Arrays.asList(StringUtil.COMMA_SEPARATED_PATTERN.split(h))).get())));

        if (isRenderedUsingXMLView(pageRequest, proxyResponse, responseContentType)) {
            renderXMLView(pageRequest, viewResponse, proxyResponse, proxiedFileURL, responseContentType);
        } else if (isRenderedUsingTextView(pageRequest, proxyResponse, responseContentType)) {
            renderTextView(pageRequest, viewResponse, proxyResponse, proxiedFileURL, responseContentType);
        } else {
            renderResourceReferenceView(pageRequest, viewResponse, proxiedFileURL, responseContentType);
        }

    } catch (WWWEEEPortal.Exception wpe) {
        LogAnnotation.annotate(wpe, "ProxyContext", proxyContext, null, false);
        LogAnnotation.annotate(wpe, "ProxyResponse", proxyResponse, null, false);
        LogAnnotation.annotate(wpe, "ProxiedFileURL", proxiedFileURL, null, false); // This wouldn't be necessary if any of the previous annotations could actually toString() themselves usefully.
        throw wpe;
    } catch (IOException ioe) {
        throw new WWWEEEPortal.OperationalException(ioe);
    }

    return;
}

From source file:net.www_eee.portal.channels.ProxyChannel.java

@Override
protected Response doResourceRequestImpl(final Page.Request pageRequest)
        throws WWWEEEPortal.Exception, WebApplicationException {
    final HttpClientContext proxyContext = doProxyRequest(pageRequest, Mode.RESOURCE);
    @SuppressWarnings("resource")
    final CloseableHttpClient proxyClient = Objects
            .requireNonNull((CloseableHttpClient) proxyContext.getAttribute(HTTP_CLIENT_CONTEXT_ID));

    final @NonNull HttpResponse proxyResponse = proxyContext.getResponse();

    try {//from  ww  w  .ja  va  2  s.c  om

        final Response.ResponseBuilder responseBuilder = Response.ok();

        responseBuilder.lastModified(getProxyResponseHeader(pageRequest, proxyResponse, "Last-Modified",
                (s) -> Date.from(ZonedDateTime.parse(s, DateTimeFormatter.RFC_1123_DATE_TIME).toInstant())));
        final MimeType responseContentType = getProxyResponseHeader(pageRequest, proxyResponse, "Content-Type",
                IOUtil::newMimeType);
        responseBuilder.type((responseContentType != null) ? RESTUtil.getMediaType(responseContentType) : null);
        responseBuilder.cacheControl(mergeCacheControl(getCacheControlDefault().orElse(null),
                getProxyResponseHeader(pageRequest, proxyResponse, "Cache-Control", CacheControl::valueOf)));
        responseBuilder.expires(getProxyResponseHeader(pageRequest, proxyResponse, "Expires",
                (s) -> Date.from(ZonedDateTime.parse(s, DateTimeFormatter.RFC_1123_DATE_TIME).toInstant())));
        responseBuilder.tag(getProxyResponseHeader(pageRequest, proxyResponse, "ETag", EntityTag::valueOf));
        responseBuilder.language(getProxyResponseHeader(pageRequest, proxyResponse, "Content-Language",
                (h) -> Locale.forLanguageTag(StringUtil.COMMA_SEPARATED_PATTERN.split("")[0]).toString()));

        final HttpEntity proxyResponseEntity = proxyResponse.getEntity();
        final Long contentLength = (proxyResponseEntity != null)
                ? Long.valueOf(proxyResponseEntity.getContentLength())
                : null;
        responseBuilder.header("Content-Length", contentLength);

        if (proxyResponseEntity != null) {
            responseBuilder.entity(HttpUtil.getDataSource(proxyResponseEntity, proxyClient));
        } else {
            try {
                proxyClient.close();
            } catch (IOException ioe) {
                throw new WWWEEEPortal.OperationalException(ioe);
            }
        }

        return responseBuilder.build();

    } catch (WWWEEEPortal.Exception wpe) {
        LogAnnotation.annotate(wpe, "ProxyContext", proxyContext, null, false);
        LogAnnotation.annotate(wpe, "ProxyResponse", proxyResponse, null, false);
        try {
            LogAnnotation.annotate(wpe, "ProxiedFileURL", HttpUtil.getRequestTargetURL(proxyContext), null,
                    false); // This wouldn't be necessary if any of the previous annotations could actually toString() themselves usefully.
        } catch (Exception e) {
        }
        throw wpe;
    }
}