public static final File writeFileItemToFolder( FileItem fileItem, File folder, boolean overwrite, boolean rename) throws IOException { if (!folder.isDirectory()) { return null; } File file = new File( URLHelper.mergePath( folder.getAbsolutePath(), StringHelper.createFileName(StringHelper.getFileNameFromPath(fileItem.getName())))); if (!file.exists()) { file.createNewFile(); } else { if (!overwrite && !rename) { throw new FileExistsException("File already exists."); } if (rename) { file = ResourceHelper.getFreeFileName(file); } } InputStream in = null; try { in = fileItem.getInputStream(); writeStreamToFile(in, file); return file; } catch (IOException e) { ResourceHelper.closeResource(in); file.delete(); throw e; } finally { ResourceHelper.closeResource(in); } }
public static String createModulePath(ContentContext ctx, String path) throws ModuleException, Exception { Module currentModule = ModulesContext.getInstance( ctx.getRequest().getSession(), GlobalContext.getInstance(ctx.getRequest())) .getCurrentModule(); String insideModulePath = URLHelper.mergePath("/" + currentModule.getModuleFolder(), currentModule.getName(), path); return insideModulePath; }
public static void downloadResource( String localDir, String baseURL, Collection<Resource> resources) throws IOException { for (Resource resource : resources) { URL url = new URL(URLHelper.mergePath(baseURL, resource.getUri())); File localFile = new File(URLHelper.mergePath(localDir, resource.getUri())); if (!localFile.exists()) { InputStream in = null; try { in = url.openStream(); ResourceHelper.writeStreamToFile(in, localFile); logger.info("download resource : " + url + " in " + localFile); } finally { if (in != null) { in.close(); } } } else { logger.warning("download url error : file already exists in local : " + localFile); } } }
public static Collection<File> getAllResources(ContentContext ctx) { GlobalContext globalContext = GlobalContext.getInstance(ctx.getRequest()); StaticConfig staticConfig = StaticConfig.getInstance(ctx.getRequest().getSession()); File staticDir = new File( URLHelper.mergePath(globalContext.getDataFolder(), staticConfig.getStaticFolder())); Collection<File> outFiles = new LinkedList<File>(); getAllResource(outFiles, staticDir); return outFiles; }
/** * extract a relative path from a full path. * * @param application the servlet context. * @param fullPath a full path * @return retrun a relative path (sample: /var/data/static/test.png -> /test.png) */ public static String extractResourceDir( StaticConfig staticConfig, GlobalContext globalContext, String fullPath) { String realPath = URLHelper.mergePath(globalContext.getDataFolder(), staticConfig.getStaticFolder()); realPath = ResourceHelper.getLinuxPath(realPath); fullPath = ResourceHelper.getLinuxPath(fullPath); if (fullPath.startsWith(realPath)) { return fullPath.substring(realPath.length()); } return fullPath; }
public AnnotatedResult(DocumentModel doc, UriInfo info, String baseURL) { this.doc = doc; this.uriInfo = info; this.baseURL = baseURL; this.backofficeURL = URLHelper.documentUrl(doc, baseURL); this.translation = new SamarTranslationAdapter(doc); if (doc.getType().equals("Video")) { videoDocument = doc.getAdapter(VideoDocument.class); webmTranscodedVideo = videoDocument.getTranscodedVideo("WebM 480p"); mp4TranscodedVideo = videoDocument.getTranscodedVideo("MP4 480p"); } else { videoDocument = null; webmTranscodedVideo = null; mp4TranscodedVideo = null; } }
/** * return a free file name. if file exist add a number as suffix. * * @param file * @return */ public static synchronized File getFreeFileName(File file) { if (!file.exists()) { return file; } File folder = file.getParentFile(); String newName = file.getName(); String ext = StringHelper.getFileExtension(file.getName()); String fileName = StringHelper.getFileNameWithoutExtension(file.getName()); for (int i = 1; i < 999999; i++) { newName = fileName + "_" + i + '.' + ext; File newFile = new File(URLHelper.mergePath(folder.getAbsolutePath(), newName)); if (!newFile.exists()) { return newFile; } } return null; }
private String getMsgListJson() { String url = URLHelper.getMentionsTimeLine(); Map<String, String> map = new HashMap<String, String>(); map.put("access_token", access_token); map.put("since_id", since_id); map.put("max_id", max_id); map.put("count", count); map.put("page", page); map.put("filter_by_author", filter_by_author); map.put("filter_by_source", filter_by_source); map.put("trim_user", trim_user); String jsonData = null; try { jsonData = HttpUtility.getInstance().executeNormalTask(HttpMethod.Get, url, map); } catch (WeiboException e) { e.printStackTrace(); // To change body of catch statement use File | Settings | File // Templates. } return jsonData; }
public String bigFileUrl(String blobPropertyName, String filename) { return URLHelper.bigFileUrl(doc, baseURL, blobPropertyName, filename); }
public static boolean isTransformURL(ContentContext ctx, String url) throws Exception { final String FAKE_FILTER = "___FAKE_FILTER___"; String startURL = URLHelper.createTransformURL(ctx, "/", FAKE_FILTER); startURL = startURL.substring(0, startURL.indexOf(FAKE_FILTER)); return url.startsWith(startURL); }
public static boolean isResourceURL(ContentContext ctx, String url) { String startURL = URLHelper.createResourceURL(ctx, "/"); return url.startsWith(startURL); }
public static String excutePost( String targetURL, String urlParameters, String contentType, String lang, String user, String pwd) { URL url; HttpURLConnection connection = null; BufferedReader rd = null; PrintWriter writer = null; if (urlParameters == null) { urlParameters = ""; } Map<String, String> params = URLHelper.getParams(urlParameters); StringBuffer encodedParam = new StringBuffer(); String sep = ""; for (Map.Entry<String, String> param : params.entrySet()) { encodedParam.append(sep); encodedParam.append(param.getKey()); encodedParam.append("="); encodedParam.append(URLEncoder.encode(param.getValue())); sep = "&"; } if (urlParameters == null) { urlParameters = ""; } try { // Create connection url = new URL(targetURL); connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Content-Type", contentType); connection.setRequestProperty("Content-Language", lang); connection.setDoOutput(true); // user authentification if (user != null && pwd != null) { connection.setRequestProperty( "Authorization", "Basic " + Base64.encodeBase64((user + ':' + pwd).getBytes())); } // Send request writer = new PrintWriter(connection.getOutputStream()); writer.write(encodedParam.toString()); writer.flush(); // Get Response InputStream is = connection.getInputStream(); String line; rd = new BufferedReader(new InputStreamReader(is)); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); PrintStream out = new PrintStream(outStream); while ((line = rd.readLine()) != null) { out.println(line); } out.close(); return new String(outStream.toByteArray()); } catch (Exception e) { e.printStackTrace(); return null; } finally { closeResource(rd, writer); if (connection != null) { connection.disconnect(); } } }