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;
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations