Example usage for org.apache.wicket.util.io IOUtils closeQuietly

List of usage examples for org.apache.wicket.util.io IOUtils closeQuietly

Introduction

In this page you can find the example usage for org.apache.wicket.util.io IOUtils closeQuietly.

Prototype

public static void closeQuietly(final Closeable closeable) 

Source Link

Document

Unconditionally close a Closeable.

Usage

From source file:com.zh.snmp.snmpweb.pages.snmp.DeviceConfigEditPanel.java

License:Open Source License

@Override
protected boolean onModalSave(AjaxRequestTarget target) {
    String errKey = null;/*from   w  w w  . j  ava 2s  .  com*/
    DeviceConfigEntity saveable = (DeviceConfigEntity) form.getDefaultModelObject();
    DeviceConfigEntity checkCode = service.findConfigEntityByCode(saveable.getId());
    if (checkCode != null && !checkCode.getId().equals(saveable.getId())) {
        errKey = "deviceConfigEntity.error.codeExists";
    } else if (saveable.getId() == null) {
        FileUpload upload = file.getFileUpload();
        InputStream is = null;
        try {
            is = upload.getInputStream();
            saveable.setSnmpDescriptor(IOUtils.toString(is));
        } catch (IOException e) {
            IOUtils.closeQuietly(is);
            errKey = "fileUpload.exception";
        }
    }
    if (errKey != null) {
        error(getString(errKey));
        target.addComponent(feedback);
        return false;
    } else {
        service.saveEntity(saveable);
        return true;
    }
}

From source file:fiftyfive.wicket.resource.MergedResourceBuilderTest.java

License:Apache License

/**
 * Download the resource at the given URI and make sure its contents
 * are identical to a merged list of files from the test fixture.
 *///from w  w  w .  ja  v a 2s . c om
protected void assertDownloaded(WicketTester tester, String uri, String... files) throws IOException {
    ByteArrayOutputStream expected = new ByteArrayOutputStream();
    for (String filename : files) {
        InputStream is = getClass().getResourceAsStream(filename);
        try {
            IOUtils.copy(is, expected);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    WicketTestUtils.assertDownloadEquals(tester, uri, expected.toByteArray());
}

From source file:fiftyfive.wicket.resource.SimpleCDNTest.java

License:Apache License

/**
 * Verify that the rewritten resource URLs still work and download the expected binary data
 * once the CDN host is stripped off (as a reverse-proxy CDN would do).
 *//*from ww  w  .j a  v a2 s  . c om*/
void assertResourcesDownload(WicketTester tester) throws Exception {
    String[] resources = new String[] { "test.css", "test.js", "test.gif" };
    for (String res : resources) {
        InputStream is = getClass().getResourceAsStream(res);
        try {
            String uri = "wicket/resource/fiftyfive.wicket.resource.SimpleCDNTestPage/" + res;
            byte[] expected = IOUtils.toByteArray(is);
            WicketTestUtils.assertDownloadEquals(tester, uri, expected);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
}

From source file:fiftyfive.wicket.test.BaseValidatorTest.java

License:Apache License

protected AbstractDocumentValidator validator(String resource) throws IOException {
    InputStream in = null;/*from   www  .  jav a2s .  c o  m*/
    try {
        in = getClass().getResourceAsStream(resource);
        String doc = IOUtils.toString(in, "UTF-8");
        AbstractDocumentValidator validator = createValidator();
        validator.parse(doc);
        return validator;
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:fiftyfive.wicket.test.dtd.XHtmlEntityResolverTest.java

License:Apache License

private void parse(String resource) throws Exception {
    InputStream in = getClass().getResourceAsStream(resource);
    try {/*from   www  . j  av  a2  s . com*/
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        fac.setValidating(true);

        DocumentBuilder builder = fac.newDocumentBuilder();
        builder.setEntityResolver(new XHtmlEntityResolver() {
            @Override
            protected void onNotFound(String pubId, String sysId) {
                XHtmlEntityResolverTest.this.missing.add(sysId);
            }
        });
        this.missing.clear();

        builder.parse(in);

        if (this.missing.size() > 0) {
            Assert.fail("Entities not found: " + this.missing);
        }
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:net.dontdrinkandroot.extensions.wicket.resource.AbstractFileThumbnailResource.java

License:Apache License

@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {
    final File file = this.resolveFile(attributes);

    final Integer width = this.resolveWidth(attributes);

    final byte[] imageBytes;
    try {/*from  ww w.j  av  a2 s .  com*/
        if (width == null) {
            final FileInputStream fis = new FileInputStream(file);
            final ByteArrayOutputStream imageBytesStream = new ByteArrayOutputStream();
            IOUtils.copy(fis, imageBytesStream);
            imageBytes = imageBytesStream.toByteArray();
            IOUtils.closeQuietly(fis);
        } else {
            final BufferedImage bufferedImage = ImageIO.read(file);
            final int oldWidth = bufferedImage.getWidth();
            final int oldHeight = bufferedImage.getHeight();
            final int newWidth = width;
            final int newHeight = newWidth * oldHeight / oldWidth;
            final BufferedImage scaledImage = new BufferedImage(newWidth, newHeight,
                    BufferedImage.TYPE_INT_RGB);
            final Graphics2D g2d = (Graphics2D) scaledImage.getGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.drawImage(bufferedImage, 0, 0, newWidth, newHeight, 0, 0, oldWidth, oldHeight, null);
            g2d.dispose();
            final ByteArrayOutputStream imageBytesStream = new ByteArrayOutputStream();
            ImageIO.write(scaledImage, "jpg", imageBytesStream);
            imageBytes = imageBytesStream.toByteArray();
        }
    } catch (final IOException e) {
        throw new WicketRuntimeException(e);
    }

    final ResourceResponse resourceResponse = new ResourceResponse() {
        @Override
        public WriteCallback getWriteCallback() {
            return new WriteCallback() {
                @Override
                public void writeData(final Attributes attributes) {
                    try {
                        this.writeStream(attributes, new ByteArrayInputStream(imageBytes));
                    } catch (IOException e) {
                        throw new WicketRuntimeException(e);
                    }
                }
            };
        }
    };
    resourceResponse.setContentType("image/jpeg");
    resourceResponse.setLastModified(Time.millis(file.lastModified()));
    resourceResponse.setCacheDuration(this.getCacheDuration());
    resourceResponse.setContentLength(imageBytes.length);

    return resourceResponse;
}

From source file:net.dontdrinkandroot.wicket.resource.AbstractFileThumbnailResource.java

License:Apache License

@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {

    final File file = this.resolveFile(attributes);

    final Integer width = this.resolveWidth(attributes);

    final byte[] imageBytes;
    try {//from   ww w  . ja v  a 2s  . c  o  m

        if (width == null) {

            final FileInputStream fis = new FileInputStream(file);
            final ByteArrayOutputStream imageBytesStream = new ByteArrayOutputStream();
            IOUtils.copy(fis, imageBytesStream);
            imageBytes = imageBytesStream.toByteArray();
            IOUtils.closeQuietly(fis);

        } else {

            final BufferedImage bufferedImage = ImageIO.read(file);
            final int oldWidth = bufferedImage.getWidth();
            final int oldHeight = bufferedImage.getHeight();
            final int newWidth = width.intValue();
            final int newHeight = newWidth * oldHeight / oldWidth;
            final BufferedImage scaledImage = new BufferedImage(newWidth, newHeight,
                    BufferedImage.TYPE_INT_RGB);
            final Graphics2D g2d = (Graphics2D) scaledImage.getGraphics();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.drawImage(bufferedImage, 0, 0, newWidth, newHeight, 0, 0, oldWidth, oldHeight, null);
            g2d.dispose();
            final ByteArrayOutputStream imageBytesStream = new ByteArrayOutputStream();
            ImageIO.write(scaledImage, "jpg", imageBytesStream);
            imageBytes = imageBytesStream.toByteArray();

        }

    } catch (final IOException e) {
        throw new WicketRuntimeException(e);
    }

    final ResourceResponse resourceResponse = new ResourceResponse() {

        @Override
        public WriteCallback getWriteCallback() {

            return new WriteCallback() {

                @Override
                public void writeData(final Attributes attributes) {

                    try {
                        this.writeStream(attributes, new ByteArrayInputStream(imageBytes));
                    } catch (IOException e) {
                        throw new WicketRuntimeException(e);
                    }
                }
            };
        }
    };
    resourceResponse.setContentType("image/jpeg");
    resourceResponse.setLastModified(Time.millis(file.lastModified()));
    resourceResponse.setCacheDuration(this.getCacheDuration());
    resourceResponse.setContentLength(imageBytes.length);

    return resourceResponse;
}

From source file:org.apache.jetspeed.security.mfa.TestCaptchaImageResource.java

License:Apache License

@Override
public void setUp() throws Exception {
    PropertiesConfiguration config = new PropertiesConfiguration();

    InputStream input = null;//from w w w .jav  a  2 s .  com

    try {
        input = Thread.currentThread().getContextClassLoader().getResourceAsStream("mfa.properties");
        config.load(input);
    } finally {
        IOUtils.closeQuietly(input);
    }

    captchaConfig = new CaptchaConfiguration(config);
    captchaConfig.setUseImageBackground(true);

    ByteArrayOutputStream output = null;

    try {
        input = Thread.currentThread().getContextClassLoader().getResourceAsStream("Jetspeed_white_sm-1.jpg");
        output = new ByteArrayOutputStream();
        IOUtils.copy(input, output);
        background = output.toByteArray();
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(input);
    }

    tempCaptchaFile = File.createTempFile("captcha-", ".jpg");
}

From source file:org.apache.jetspeed.security.mfa.TestCaptchaImageResource.java

License:Apache License

public void testCaptchaImageData() throws Exception {
    CaptchaImageResource cir = new CaptchaImageResource(captchaConfig);
    cir.setBackgroundImage(background);//w  ww . j  a va 2  s . c om
    cir.init();

    OutputStream output = null;

    try {
        output = new FileOutputStream(tempCaptchaFile);
        IOUtils.write(cir.getImageBytes(), output);
    } finally {
        IOUtils.closeQuietly(output);
    }

    assertTrue(tempCaptchaFile.length() > 0);
}

From source file:org.apache.syncope.client.console.resources.FilesystemResource.java

License:Apache License

@Override
protected ResourceResponse newResourceResponse(final Attributes attributes) {
    ResourceResponse response = new ResourceResponse();

    final File baseDir = new File(basePath);
    if (baseDir.exists() && baseDir.canRead() && baseDir.isDirectory()) {
        String reqPath = attributes.getRequest().getUrl().getPath();
        final String subPath = reqPath.substring(reqPath.indexOf(baseCtx) + baseCtx.length()).replace('/',
                File.separatorChar);
        LOG.debug("Request for {}", subPath);

        response.setWriteCallback(new WriteCallback() {

            @Override/*  w  w w.  j a va2s  .co m*/
            public void writeData(final Attributes attributes) throws IOException {
                FileInputStream resourceIS = null;
                try {
                    resourceIS = new FileInputStream(new File(baseDir, subPath));
                    IOUtils.copy(resourceIS, attributes.getResponse().getOutputStream());
                } catch (IOException e) {
                    LOG.error("Could not read from {}", baseDir.getAbsolutePath() + subPath, e);
                } finally {
                    IOUtils.closeQuietly(resourceIS);
                }
            }
        });
    } else {
        LOG.error("{} not found, not readable or not a directory", basePath);
    }

    return response;
}