protected void setLocationFromScout() { m_browserExtension.clearResourceCache(); m_browserExtension.clearLocalHyperlinkCache(); String location = getScoutObject().getLocation(); RemoteFile r = getScoutObject().getValue(); if (location == null && r != null && r.exists()) { try { if (r.getName().matches(".*\\.(zip|jar)")) { location = registerResourcesInZip(r); } else { String content = IOUtility.getContent(r.getDecompressedReader()); content = m_browserExtension.adaptLocalHyperlinks(content, 1); location = m_browserExtension.addResource( r.getName(), new ByteArrayInputStream(content.getBytes("UTF-8"))); } // Prevent caching by making the request unique if (location != null) { location += "?nocache=" + System.currentTimeMillis(); } } catch (Throwable t) { LOG.error("preparing html content for " + r, t); } } m_currentLocation = location; if (m_currentLocation != null) { getUiField().setUrl(m_currentLocation); } else { getUiField().setText(""); } }
@Override public File getRemoteFile(String dir, String simpleName, Locale locale, boolean checkCache) throws ProcessingException { RemoteFile spec = null; File f = null; if (locale != null && simpleName != null && simpleName.lastIndexOf(".") != -1) { String filename = simpleName; String language = locale.toString().replaceAll("__", "_"); String prefix = filename.substring(0, filename.lastIndexOf(".")) + "_"; String suffix = filename.substring(filename.lastIndexOf(".")); filename = prefix + language + suffix; File test = getFileLocation(dir, filename, false); while (!test.exists()) { if (language.indexOf("_") == -1) { filename = simpleName; break; } language = language.substring(0, language.lastIndexOf("_")); filename = prefix + language + suffix; test = getFileLocation(dir, filename, false); } f = getFileLocation(dir, filename, false); spec = new RemoteFile(dir, filename, locale, 0L); } else { f = getFileLocation(dir, simpleName, false); spec = new RemoteFile(dir, simpleName, locale, 0L); } if (f.exists()) { spec.setLastModified(f.lastModified()); } // if (checkCache && OfflineState.isOnlineInCurrentThread()) { IRemoteFileService svc = SERVICES.getService(IRemoteFileService.class); spec = svc.getRemoteFile(spec); try { if (spec.getName() != null && !spec.getName().equalsIgnoreCase(f.getName())) { if (locale != null && f.getName().length() > spec.getName().length()) { // if local file has longer name (including locale), this means that // this file was deleted on the server f.delete(); } f = getFileLocation(spec.getDirectory(), spec.getName(), false); } if (spec.exists() && spec.hasContent()) { spec.writeData(new FileOutputStream(f)); f.setLastModified(spec.getLastModified()); } else if (!spec.exists()) { f.delete(); } } catch (IOException e) { throw new ProcessingException("error writing remote file in local store", e); } } return f; }
private String registerResourcesInZip(RemoteFile zipFile) throws ProcessingException, IOException, UnsupportedEncodingException, FileNotFoundException { String location = null; File tempDir = IOUtility.createTempDirectory("browser"); try { zipFile.writeZipContentToDirectory(tempDir); String simpleName = zipFile.getName().replaceAll("\\.(zip|jar)", ".htm"); // rewrite local urls and register resource int prefixLen = tempDir.getAbsolutePath().length() + 1; for (File f : IOUtility.listFilesInSubtree(tempDir, null)) { if (f.isFile()) { String path = f.getAbsolutePath().substring(prefixLen); if (path.toLowerCase().matches(".*\\.(htm|html)")) { String content = IOUtility.getContent(new InputStreamReader(new FileInputStream(f), "UTF-8")); content = m_browserExtension.adaptLocalHyperlinks(content, 1); if (location == null && path.startsWith(simpleName)) { // this is the index.html location = m_browserExtension.addResource( simpleName, new ByteArrayInputStream(content.getBytes("UTF-8"))); } else { m_browserExtension.addResource( path, new ByteArrayInputStream(content.getBytes("UTF-8"))); } } else if (path.toLowerCase().matches(".*\\.(svg)")) { String content = IOUtility.getContent(new InputStreamReader(new FileInputStream(f))); content = m_browserExtension.adaptLocalHyperlinks(content, 1); m_browserExtension.addResource( path, new ByteArrayInputStream(content.getBytes("UTF-8"))); } else { m_browserExtension.addResource(path, new FileInputStream(f)); } } } } finally { if (tempDir != null) { IOUtility.deleteDirectory(tempDir); } } return location; }
private void syncRemoteFilesInternal( String serverFolderPath, FilenameFilter filter, boolean useServerFolderStructureOnClient) throws ProcessingException { IRemoteFileService svc = SERVICES.getService(IRemoteFileService.class); String[][] realFiles = getFiles(serverFolderPath, filter, useServerFolderStructureOnClient); RemoteFile[] existingFileInfoOnClient = new RemoteFile[realFiles.length]; for (int i = 0; i < realFiles.length; i++) { RemoteFile rf = new RemoteFile(realFiles[i][0], realFiles[i][1], 0); String dir = m_rootPath == null ? realFiles[i][0] : ""; File f = getFileLocation(dir, realFiles[i][1], false); if (f.exists()) { rf.setLastModified(f.lastModified()); } existingFileInfoOnClient[i] = rf; } existingFileInfoOnClient = svc.getRemoteFiles(serverFolderPath, filter, existingFileInfoOnClient); for (RemoteFile spec : existingFileInfoOnClient) { String fileDirectory = useServerFolderStructureOnClient ? spec.getDirectory() : null; File f = getFileLocation(fileDirectory, spec.getName(), false); if (spec.exists() && spec.hasContent()) { try { if (spec.hasMoreParts()) { // file is splitted - get all parts int counter = 0; long fileDate = spec.getLastModified(); File part = getFileLocation(fileDirectory, spec.getName() + "." + counter, false); spec.writeData(new FileOutputStream(part)); part.setLastModified(fileDate); RemoteFile specPart = spec; while (specPart.hasMoreParts()) { counter++; part = getFileLocation(fileDirectory, spec.getName() + "." + counter, false); if (!part.exists() || fileDate != part.lastModified()) { specPart = svc.getRemoteFilePart(spec, counter); specPart.writeData(new FileOutputStream(part)); part.setLastModified(fileDate); } else { // resuming canceled part: nothing to do } } // put together counter = 0; f = getFileLocation(fileDirectory, spec.getName(), false); OutputStream out = new FileOutputStream(f); part = getFileLocation(fileDirectory, spec.getName() + "." + counter, false); while (part.exists()) { InputStream in = new FileInputStream(part); byte[] buf = new byte[102400]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.flush(); in.close(); part.delete(); counter++; part = getFileLocation(fileDirectory, spec.getName() + "." + counter, false); } out.close(); f.setLastModified(fileDate); } else { // normal files spec.writeData(new FileOutputStream(f)); f.setLastModified(spec.getLastModified()); } } catch (IOException e) { throw new ProcessingException("error writing remote file in local store", e); } } else if (!spec.exists()) { f.delete(); } } }