Search in sources :

Example 26 with BinaryResource

use of org.eclipse.scout.rt.platform.resource.BinaryResource in project scout.rt by eclipse.

the class ResourceServlet method writeStaticResource.

private boolean writeStaticResource(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    String pathInfo = req.getPathInfo();
    if (StringUtility.isNullOrEmpty(pathInfo) || pathInfo.endsWith("/")) {
        pathInfo = "/index.html";
    }
    URL url = null;
    String contentType = null;
    if (m_warPath != null) {
        ServletContext servletContext = getServletContext();
        String resourcePath = m_warPath + pathInfo;
        url = servletContext.getResource(resourcePath);
        contentType = FileUtility.getMimeType(resourcePath);
    }
    // 
    if (url == null) {
        return false;
    }
    long lastModified;
    int contentLength;
    URLConnection connection = url.openConnection();
    lastModified = connection.getLastModified();
    contentLength = connection.getContentLength();
    BinaryResource res = BinaryResources.create().withFilename(pathInfo).withContentType(contentType).withLastModified(lastModified).build();
    HttpCacheObject obj = new HttpCacheObject(new HttpCacheKey(pathInfo), res);
    if (BEANS.get(HttpCacheControl.class).checkAndSetCacheHeaders(req, resp, obj)) {
        return true;
    }
    try (InputStream is = connection.getInputStream()) {
        @SuppressWarnings("resource") OutputStream os = resp.getOutputStream();
        byte[] buffer = new byte[8192];
        int bytesRead = is.read(buffer);
        int writtenContentLength = 0;
        while (bytesRead != -1) {
            os.write(buffer, 0, bytesRead);
            writtenContentLength += bytesRead;
            bytesRead = is.read(buffer);
        }
        if (contentLength == -1 || contentLength != writtenContentLength) {
            resp.setContentLength(writtenContentLength);
        }
    }
    return true;
}
Also used : HttpCacheControl(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheControl) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) URL(java.net.URL) URLConnection(java.net.URLConnection) BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) HttpCacheKey(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey) ServletContext(javax.servlet.ServletContext) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)

Example 27 with BinaryResource

use of org.eclipse.scout.rt.platform.resource.BinaryResource in project scout.rt by eclipse.

the class AbstractForm method doImportXml.

@Override
public void doImportXml() {
    try {
        List<BinaryResource> a = new FileChooser(Collections.singletonList("xml"), false).startChooser();
        if (a.size() == 1) {
            BinaryResource newPath = a.get(0);
            try (InputStream in = new ByteArrayInputStream(newPath.getContent())) {
                // NOSONAR
                Document doc = XmlUtility.getXmlDocument(in);
                // load xml to search
                loadFromXml(doc.getDocumentElement());
            } catch (Exception e) {
                LOG.warn("Could not load XML from file: {}", newPath, e);
                MessageBoxes.createOk().withDisplayParent(this).withHeader(TEXTS.get("LoadFormXmlFailedText")).show();
            }
        }
    } catch (Exception e) {
        BEANS.get(ExceptionHandler.class).handle(e);
    }
}
Also used : BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileChooser(org.eclipse.scout.rt.client.ui.basic.filechooser.FileChooser) Document(org.w3c.dom.Document) PlatformException(org.eclipse.scout.rt.platform.exception.PlatformException) ProcessingException(org.eclipse.scout.rt.platform.exception.ProcessingException) VetoException(org.eclipse.scout.rt.platform.exception.VetoException)

Example 28 with BinaryResource

use of org.eclipse.scout.rt.platform.resource.BinaryResource in project scout.rt by eclipse.

the class DynamicResourceLoader method loadResource.

@Override
public HttpCacheObject loadResource(HttpCacheKey cacheKey) {
    DynamicResourceInfo info = createDynamicResourceInfo(cacheKey);
    if (info == null) {
        LOG.warn("invalid dynamic-resource request received.", new Exception("origin"));
        return null;
    }
    IBinaryResourceProvider provider = getBinaryResourceProvider(info.getUiSession(), info.getJsonAdapterId());
    if (provider == null) {
        return null;
    }
    BinaryResourceHolder localResourceHolder = provider.provideBinaryResource(info.getFileName());
    if (localResourceHolder == null || localResourceHolder.get() == null) {
        return null;
    }
    BinaryResource localResource = localResourceHolder.get();
    BinaryResource httpResource = localResource.createAlias(cacheKey.getResourcePath());
    HttpCacheObject httpCacheObject = new HttpCacheObject(cacheKey, httpResource);
    for (IHttpResponseInterceptor interceptor : localResourceHolder.getHttpResponseInterceptors()) {
        httpCacheObject.addHttpResponseInterceptor(interceptor);
    }
    return httpCacheObject;
}
Also used : IBinaryResourceProvider(org.eclipse.scout.rt.ui.html.res.IBinaryResourceProvider) BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) IHttpResponseInterceptor(org.eclipse.scout.rt.server.commons.servlet.cache.IHttpResponseInterceptor) IOException(java.io.IOException) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject) BinaryResourceHolder(org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder)

Example 29 with BinaryResource

use of org.eclipse.scout.rt.platform.resource.BinaryResource in project scout.rt by eclipse.

the class ResourceRequestHandler method handleGet.

@Override
public boolean handleGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String pathInfoEx = resolvePathInfoEx(req);
    // Create loader for the requested resource type
    IResourceLoader resourceLoader = null;
    for (ResourceLoaders f : m_resourceLoaders) {
        resourceLoader = f.create(req, pathInfoEx);
        if (resourceLoader != null) {
            break;
        }
    }
    if (resourceLoader == null) {
        return false;
    }
    HttpCacheObject resource = resolveResourceFromCache(req, pathInfoEx, resourceLoader);
    // check resource existence (also ignore resources without content, to prevent invalid "content-length" header and NPE in write() method)
    if (resource == null || resource.getResource() == null || resource.getResource().getContent() == null) {
        return false;
    }
    // cached in browser? -> returns 304 if the resource has not been modified
    if (m_httpCacheControl.checkAndSetCacheHeaders(req, resp, resource)) {
        return true;
    }
    BinaryResource binaryResource = resource.getResource();
    // set the resp headers only if no 304 (according to spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5)
    setHttpResponseHeaders(resp, binaryResource);
    // Apply response interceptors
    resource.applyHttpResponseInterceptors(req, resp);
    if (!"HEAD".equals(req.getMethod())) {
        resp.getOutputStream().write(binaryResource.getContent());
    }
    return true;
}
Also used : BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource) IResourceLoader(org.eclipse.scout.rt.ui.html.res.loader.IResourceLoader) ResourceLoaders(org.eclipse.scout.rt.ui.html.res.loader.ResourceLoaders) HttpCacheObject(org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)

Example 30 with BinaryResource

use of org.eclipse.scout.rt.platform.resource.BinaryResource in project scout.rt by eclipse.

the class AbstractBrowserField method resolveBinaryResource.

protected BinaryResource resolveBinaryResource(String filename) {
    if (filename == null) {
        return null;
    }
    BinaryResource binaryResource = getBinaryResource();
    if (binaryResource != null && ObjectUtility.equals(binaryResource.getFilename(), filename)) {
        return binaryResource;
    }
    Set<BinaryResource> attachments = getAttachments();
    if (attachments != null) {
        for (BinaryResource attachment : attachments) {
            if (ObjectUtility.equals(attachment.getFilename(), filename)) {
                return attachment;
            }
        }
    }
    LOG.warn("Could not resolve binary resource for filename: {}", filename);
    return null;
}
Also used : BinaryResource(org.eclipse.scout.rt.platform.resource.BinaryResource)

Aggregations

BinaryResource (org.eclipse.scout.rt.platform.resource.BinaryResource)46 Test (org.junit.Test)26 HttpCacheObject (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheObject)19 HttpCacheKey (org.eclipse.scout.rt.server.commons.servlet.cache.HttpCacheKey)15 ProcessingException (org.eclipse.scout.rt.platform.exception.ProcessingException)4 BinaryResourceHolder (org.eclipse.scout.rt.ui.html.res.BinaryResourceHolder)4 InputStream (java.io.InputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 PlatformException (org.eclipse.scout.rt.platform.exception.PlatformException)2 JSONObject (org.json.JSONObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 Field (java.lang.reflect.Field)1