Example usage for com.liferay.portal.kernel.webdav Resource getContentAsStream

List of usage examples for com.liferay.portal.kernel.webdav Resource getContentAsStream

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.webdav Resource getContentAsStream.

Prototype

public InputStream getContentAsStream() throws WebDAVException;

Source Link

Usage

From source file:it.smc.calendar.sync.caldav.methods.GetMethodImpl.java

License:Open Source License

@Override
public int process(WebDAVRequest webDAVRequest) throws WebDAVException {
    InputStream is = null;//from   w  w  w .j a v a2s .  c  o m

    try {
        WebDAVStorage storage = webDAVRequest.getWebDAVStorage();
        HttpServletRequest request = webDAVRequest.getHttpServletRequest();
        HttpServletResponse response = webDAVRequest.getHttpServletResponse();

        Resource resource = storage.getResource(webDAVRequest);

        if (resource == null) {
            return HttpServletResponse.SC_NOT_FOUND;
        }

        try {
            is = resource.getContentAsStream();
        } catch (Exception e) {
            if (_log.isErrorEnabled()) {
                _log.error(e.getMessage());
            }
        }

        if (is != null) {
            try {
                ServletResponseUtil.sendFile(request, response, resource.getDisplayName(), is,
                        resource.getSize(), resource.getContentType());
            } catch (Exception e) {
                if (_log.isWarnEnabled()) {
                    _log.warn(e);
                }
            }

            return HttpServletResponse.SC_OK;
        }

        return HttpServletResponse.SC_NOT_FOUND;
    } catch (ResourceNotFoundException rnfe) {
        return HttpServletResponse.SC_NOT_FOUND;
    } catch (WebDAVException pe) {
        if (pe.getCause() instanceof PrincipalException) {
            return HttpServletResponse.SC_UNAUTHORIZED;
        } else if (pe.getCause() instanceof ResourceNotFoundException) {
            return HttpServletResponse.SC_NOT_FOUND;
        }

        throw pe;
    } catch (Exception e) {
        throw new WebDAVException(e);
    }
}