List of usage examples for com.liferay.portal.kernel.util FileUtil createTempFile
public static File createTempFile()
From source file:com.liferay.document.library.document.conversion.internal.DocumentHTMLProcessor.java
License:Open Source License
public InputStream process(InputStream inputStream) { File tempFile = null;//from w w w .j a va 2s . c om InputStream processedInputStream = null; Scanner scanner = null; String replacement = ""; try { scanner = new Scanner(inputStream); scanner.useDelimiter(">"); tempFile = FileUtil.createTempFile(); long userId = PrincipalThreadLocal.getUserId(); String imageRequestToken = ImageRequestTokenUtil.createToken(userId); while (scanner.hasNext()) { String token = scanner.next(); if (Validator.isNotNull(token)) { token += ">"; replacement = token.replaceAll(_DOCUMENTS_REGEX, "$1&auth_token=" + imageRequestToken + "$3"); replacement = replacement.replaceAll(_IMAGE_REGEX, "$1&auth_token=" + imageRequestToken + "$3"); replacement = replacement.replaceAll(_PORTLET_FILE_ENTRY_REGEX, "$1?auth_token=" + imageRequestToken + "$3"); replacement = replacement.replaceAll(_WIKI_PAGE_ATTACHMENT_REGEX, "$1$3&auth_token=" + imageRequestToken + "$6"); FileUtil.write(tempFile, replacement, true, true); } } processedInputStream = new AutoDeleteFileInputStream(tempFile); } catch (Exception e) { _log.error(e, e); } finally { scanner.close(); } return processedInputStream; }
From source file:com.liferay.events.global.mobile.portlet.PollsPortlet.java
License:Open Source License
@Override public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException { // do search and return result String cmd = ParamUtil.getString(request, "cmd"); long questionId = ParamUtil.getLong(request, "questionId"); EventPollQuestion question;// ww w. jav a 2s . c o m List<EventPollAnswer> answers; try { question = EventPollQuestionLocalServiceUtil.getEventPollQuestion(questionId); answers = EventPollAnswerLocalServiceUtil.getAllAnswerObjs(questionId); } catch (SystemException e) { throw new PortletException("Cannot get answers for questionId " + questionId); } catch (PortalException e) { throw new PortletException("Cannot get question or answers for questionId " + questionId); } if (Validator.equals(cmd, "exportAnswersCSV")) { File f = FileUtil.createTempFile(); CSVWriter writer = new CSVWriter(new FileWriter(f), ','); // find out all headers List<String> headers = new ArrayList<String>(); headers.add("ID"); headers.add("RAW ANSWER"); Set<String> payloadHeaders = new HashSet<String>(); for (EventPollAnswer answer : answers) { JSONObject payloadObj = null; try { payloadObj = JSONFactoryUtil.createJSONObject(answer.getPayload()); if (Validator.isNull(payloadObj)) { continue; } } catch (JSONException e) { throw new PortletException("cannot read payload: " + answer.getPayload()); } Iterator<String> keyIt = payloadObj.keys(); while (keyIt.hasNext()) { String key = keyIt.next(); payloadHeaders.add(key); } } headers.addAll(payloadHeaders); Map<String, Integer> headerCols = new HashMap<String, Integer>(); for (int i = 0; i < headers.size(); i++) { headerCols.put(headers.get(i), i); } // now print them writer.writeNext(headers.toArray(new String[] {})); for (EventPollAnswer answer : answers) { List<String> vals = new ArrayList<String>(); JSONObject payloadObj = null; try { payloadObj = JSONFactoryUtil.createJSONObject(answer.getPayload()); if (Validator.isNull(payloadObj)) { continue; } } catch (JSONException e) { throw new PortletException("cannot read payload: " + answer.getPayload()); } for (String headerCol : headers) { String val; if (headerCol.equals("ID")) { val = String.valueOf(answer.getAnswerId()); } else if (headerCol.equals("RAW ANSWER")) { val = String.valueOf(answer.getAnswer()); } else { val = payloadObj.getString(headerCol); } if (Validator.isNull(val)) { val = ""; } vals.add(val); } writer.writeNext(vals.toArray(new String[] {})); } writer.flush(); writer.close(); PortletResponseUtil.sendFile(request, response, question.getShortTitle().replaceAll("[^0-9A-Za-z]", "-") + ".csv", new FileInputStream(f)); } else if (Validator.equals(cmd, "exportAnswersXLSX")) { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Poll Answers"); Row headerRow = sheet.createRow(0); Cell headerCell = headerRow.createCell(0); headerCell.setCellValue("ID"); headerCell = headerRow.createCell(1); headerCell.setCellValue("Raw Answer"); HashMap<String, Integer> rowMap = new HashMap<String, Integer>(); int currentRow = 1; int nextHeaderCol = 2; for (EventPollAnswer answer : answers) { Row row = sheet.createRow(currentRow); currentRow++; JSONObject payloadObj = null; long answerId = answer.getAnswerId(); Cell idCell = row.createCell(0); idCell.setCellValue(String.valueOf(answerId)); try { payloadObj = JSONFactoryUtil.createJSONObject(answer.getPayload()); if (Validator.isNull(payloadObj)) { continue; } } catch (JSONException e) { throw new PortletException("cannot read payload: " + answer.getPayload()); } Cell answerCell = row.createCell(1); answerCell.setCellValue(String.valueOf(answer.getAnswer())); Iterator<String> keyIt = payloadObj.keys(); while (keyIt.hasNext()) { String key = keyIt.next(); Integer headerCol = rowMap.get(key); if (Validator.isNull(headerCol)) { rowMap.put(key, nextHeaderCol); Cell nextHeaderCell = headerRow.createCell(nextHeaderCol); nextHeaderCell.setCellValue(key.toUpperCase()); headerCol = nextHeaderCol; nextHeaderCol++; } Cell cell = row.createCell(headerCol); cell.setCellValue(payloadObj.getString(key)); } } File f = FileUtil.createTempFile(); FileOutputStream fos = new FileOutputStream(f); workbook.write(fos); fos.flush(); fos.close(); PortletResponseUtil.sendFile(request, response, question.getShortTitle().replaceAll("[^0-9A-Za-z]", "-") + ".xlsx", new FileInputStream(f)); } }
From source file:com.liferay.image.editor.hook.action.ActionUtil.java
License:Open Source License
public static File getImageFromBlob(String blob, String formatName) throws Exception { blob = blob.substring(blob.indexOf(StringPool.COMMA) + 1); byte[] decodedBytes = Base64.decode(blob); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decodedBytes); ImageInputStream imageInputStream = ImageIO.createImageInputStream(byteArrayInputStream); Iterator<?> readerIterator = ImageIO.getImageReadersByFormatName(formatName); ImageReader reader = (ImageReader) readerIterator.next(); reader.setInput(imageInputStream, true); ImageReadParam defaultReadParam = reader.getDefaultReadParam(); BufferedImage bufferedImage = reader.read(0, defaultReadParam); File imageFile = FileUtil.createTempFile(); ImageIO.write(bufferedImage, formatName, imageFile); return imageFile; }
From source file:com.liferay.mail.service.impl.AttachmentLocalServiceImpl.java
License:Open Source License
public File getFile(long attachmentId) throws PortalException { try {/*from w ww. j a v a 2 s . c o m*/ File file = FileUtil.createTempFile(); FileUtil.write(file, getInputStream(attachmentId)); return file; } catch (IOException ioe) { throw new SystemException(ioe); } }
From source file:com.liferay.marketplace.store.portlet.StorePortlet.java
License:Open Source License
public void downloadApp(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); String token = ParamUtil.getString(actionRequest, "token"); long remoteAppId = ParamUtil.getLong(actionRequest, "appId"); String url = ParamUtil.getString(actionRequest, "url"); String version = ParamUtil.getString(actionRequest, "version"); if (!url.startsWith(PortletPropsValues.MARKETPLACE_URL)) { JSONObject jsonObject = getAppJSONObject(remoteAppId); jsonObject.put("cmd", "downloadApp"); jsonObject.put("message", "fail"); writeJSON(actionRequest, actionResponse, jsonObject); return;//www . java 2 s .c o m } url = getRemoteAppPackageURL(themeDisplay.getCompanyId(), themeDisplay.getUserId(), token, url); URL urlObj = new URL(url); File tempFile = null; try { InputStream inputStream = urlObj.openStream(); tempFile = FileUtil.createTempFile(); FileUtil.write(tempFile, inputStream); App app = AppServiceUtil.updateApp(remoteAppId, version, tempFile); JSONObject jsonObject = getAppJSONObject(app.getRemoteAppId()); jsonObject.put("cmd", "downloadApp"); jsonObject.put("message", "success"); writeJSON(actionRequest, actionResponse, jsonObject); } finally { if (tempFile != null) { tempFile.delete(); } } }
From source file:com.liferay.marketplace.store.portlet.StorePortlet.java
License:Open Source License
public void updateApp(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); String token = ParamUtil.getString(actionRequest, "token"); long remoteAppId = ParamUtil.getLong(actionRequest, "appId"); String version = ParamUtil.getString(actionRequest, "version"); String url = ParamUtil.getString(actionRequest, "url"); String orderUuid = ParamUtil.getString(actionRequest, "orderUuid"); String productEntryName = ParamUtil.getString(actionRequest, "productEntryName"); if (!url.startsWith(PortletPropsValues.MARKETPLACE_URL)) { JSONObject jsonObject = getAppJSONObject(remoteAppId); jsonObject.put("cmd", "downloadApp"); jsonObject.put("message", "fail"); writeJSON(actionRequest, actionResponse, jsonObject); return;/* w ww. j av a2s .c om*/ } url = getRemoteAppPackageURL(themeDisplay.getCompanyId(), themeDisplay.getUserId(), token, url); URL urlObj = new URL(url); File tempFile = null; try { InputStream inputStream = urlObj.openStream(); tempFile = FileUtil.createTempFile(); FileUtil.write(tempFile, inputStream); AppServiceUtil.updateApp(remoteAppId, version, tempFile); if (Validator.isNull(orderUuid) && Validator.isNotNull(productEntryName)) { orderUuid = MarketplaceLicenseUtil.getOrder(productEntryName); } if (Validator.isNotNull(orderUuid)) { MarketplaceLicenseUtil.registerOrder(orderUuid, productEntryName); } AppServiceUtil.installApp(remoteAppId); JSONObject jsonObject = getAppJSONObject(remoteAppId); jsonObject.put("cmd", "updateApp"); jsonObject.put("message", "success"); writeJSON(actionRequest, actionResponse, jsonObject); } finally { if (tempFile != null) { tempFile.delete(); } } }
From source file:com.liferay.marketplace.store.web.internal.portlet.MarketplaceStorePortlet.java
License:Open Source License
public void downloadApp(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { long appPackageId = ParamUtil.getLong(actionRequest, "appPackageId"); boolean unlicensed = ParamUtil.getBoolean(actionRequest, "unlicensed"); File file = null;// ww w . j av a 2 s. c o m try { file = FileUtil.createTempFile(); downloadApp(actionRequest, actionResponse, appPackageId, unlicensed, file); App app = _appService.updateApp(file); JSONObject jsonObject = getAppJSONObject(app.getRemoteAppId()); jsonObject.put("cmd", "downloadApp"); jsonObject.put("message", "success"); writeJSON(actionRequest, actionResponse, jsonObject); } finally { if (file != null) { file.delete(); } } }
From source file:com.liferay.marketplace.store.web.internal.portlet.MarketplaceStorePortlet.java
License:Open Source License
public void updateApp(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { long appPackageId = ParamUtil.getLong(actionRequest, "appPackageId"); boolean unlicensed = ParamUtil.getBoolean(actionRequest, "unlicensed"); String orderUuid = ParamUtil.getString(actionRequest, "orderUuid"); String productEntryName = ParamUtil.getString(actionRequest, "productEntryName"); File file = null;//from ww w.j av a2s. co m try { file = FileUtil.createTempFile(); downloadApp(actionRequest, actionResponse, appPackageId, unlicensed, file); App app = _appService.updateApp(file); if (Validator.isNull(orderUuid) && Validator.isNotNull(productEntryName)) { orderUuid = MarketplaceLicenseUtil.getOrder(productEntryName); } if (Validator.isNotNull(orderUuid)) { MarketplaceLicenseUtil.registerOrder(orderUuid, productEntryName); } _appService.installApp(app.getRemoteAppId()); JSONObject jsonObject = getAppJSONObject(app.getRemoteAppId()); jsonObject.put("cmd", "updateApp"); jsonObject.put("message", "success"); writeJSON(actionRequest, actionResponse, jsonObject); } finally { if (file != null) { file.delete(); } } }
From source file:com.liferay.marketplace.store.web.internal.portlet.MarketplaceStorePortlet.java
License:Open Source License
public void updateApps(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { JSONObject jsonObject = JSONFactoryUtil.createJSONObject(); jsonObject.put("cmd", "updateApps"); jsonObject.put("message", "success"); if (_reentrantLock.tryLock()) { try {/*w ww.jav a 2 s . c o m*/ long[] appPackageIds = ParamUtil.getLongValues(actionRequest, "appPackageIds"); JSONArray jsonArray = JSONFactoryUtil.createJSONArray(); for (long appPackageId : appPackageIds) { File file = null; try { file = FileUtil.createTempFile(); downloadApp(actionRequest, actionResponse, appPackageId, false, file); App app = _appService.updateApp(file); _appService.installApp(app.getRemoteAppId()); jsonArray.put(getAppJSONObject(app)); } catch (Exception e) { jsonObject.put("message", "failed"); } finally { if (file != null) { file.delete(); } } } jsonObject.put("updatedApps", jsonArray); } finally { _reentrantLock.unlock(); } } else { jsonObject.put("message", "failed"); } writeJSON(actionRequest, actionResponse, jsonObject); }
From source file:com.liferay.portlet.documentlibrary.service.impl.DLSyncServiceImpl.java
License:Open Source License
public InputStream getFileDeltaAsStream(long fileEntryId, String sourceVersion, String destinationVersion) throws PortalException, SystemException { InputStream deltaInputStream = null; FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId); InputStream sourceInputStream = null; File sourceFile = FileUtil.createTempFile(); FileInputStream sourceFileInputStream = null; FileChannel sourceFileChannel = null; File checksumsFile = FileUtil.createTempFile(); OutputStream checksumsOutputStream = null; WritableByteChannel checksumsWritableByteChannel = null; try {/*from www .j a v a2 s . c o m*/ sourceInputStream = fileEntry.getContentStream(sourceVersion); FileUtil.write(sourceFile, sourceInputStream); sourceFileInputStream = new FileInputStream(sourceFile); sourceFileChannel = sourceFileInputStream.getChannel(); checksumsOutputStream = new FileOutputStream(checksumsFile); checksumsWritableByteChannel = Channels.newChannel(checksumsOutputStream); ByteChannelWriter checksumsByteChannelWriter = new ByteChannelWriter(checksumsWritableByteChannel); DeltaUtil.checksums(sourceFileChannel, checksumsByteChannelWriter); checksumsByteChannelWriter.finish(); } catch (Exception e) { throw new PortalException(e); } finally { StreamUtil.cleanUp(sourceFileInputStream); StreamUtil.cleanUp(sourceFileChannel); StreamUtil.cleanUp(checksumsOutputStream); StreamUtil.cleanUp(checksumsWritableByteChannel); FileUtil.delete(sourceFile); } InputStream destinationInputStream = null; ReadableByteChannel destinationReadableByteChannel = null; InputStream checksumsInputStream = null; ReadableByteChannel checksumsReadableByteChannel = null; OutputStream deltaOutputStream = null; WritableByteChannel deltaOutputStreamWritableByteChannel = null; try { destinationInputStream = fileEntry.getContentStream(destinationVersion); destinationReadableByteChannel = Channels.newChannel(destinationInputStream); checksumsInputStream = new FileInputStream(checksumsFile); checksumsReadableByteChannel = Channels.newChannel(checksumsInputStream); ByteChannelReader checksumsByteChannelReader = new ByteChannelReader(checksumsReadableByteChannel); File deltaFile = FileUtil.createTempFile(); deltaOutputStream = new FileOutputStream(deltaFile); deltaOutputStreamWritableByteChannel = Channels.newChannel(deltaOutputStream); ByteChannelWriter deltaByteChannelWriter = new ByteChannelWriter(deltaOutputStreamWritableByteChannel); DeltaUtil.delta(destinationReadableByteChannel, checksumsByteChannelReader, deltaByteChannelWriter); deltaByteChannelWriter.finish(); deltaInputStream = new FileInputStream(deltaFile); } catch (Exception e) { throw new PortalException(e); } finally { StreamUtil.cleanUp(destinationInputStream); StreamUtil.cleanUp(destinationReadableByteChannel); StreamUtil.cleanUp(checksumsInputStream); StreamUtil.cleanUp(checksumsReadableByteChannel); StreamUtil.cleanUp(deltaOutputStream); StreamUtil.cleanUp(deltaOutputStreamWritableByteChannel); FileUtil.delete(checksumsFile); } return deltaInputStream; }