List of usage examples for com.liferay.portal.kernel.util FileUtil read
public static String read(String fileName) throws IOException
From source file:com.liferay.alloy.tools.xmlbuilder.XMLBuilder.java
License:Open Source License
public XMLBuilder(String componentsJSON, String componentsXML, String componentExcluded) throws Exception { if (FileUtil.getFile() == null) { (new FileUtil()).setFile(new FileImpl()); }// w w w .j ava 2s. c o m if (SAXReaderUtil.getSAXReader() == null) { (new SAXReaderUtil()).setSAXReader(new SAXReaderImpl()); } _componentJSON = componentsJSON; _componentXML = componentsXML; _componentExcluded = Arrays.asList(StringUtil.split(componentExcluded)); _fileXML = new File(_componentXML); _fileJSON = new File(_componentJSON); _json = new JSONObject(FileUtil.read(_fileJSON)); _classMapJSON = _json.getJSONObject("classmap"); _create(); }
From source file:com.liferay.calendar.portlet.CalendarPortlet.java
License:Open Source License
protected void serveImportCalendar(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception { UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(resourceRequest); ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY); long calendarId = ParamUtil.getLong(uploadPortletRequest, "calendarId"); File file = uploadPortletRequest.getFile("file"); String data = FileUtil.read(file); JSONObject jsonObject = JSONFactoryUtil.createJSONObject(); if (Validator.isNotNull(data)) { try {/* w ww. j a v a 2s . c o m*/ CalendarDataHandler calendarDataHandler = CalendarDataHandlerFactory .getCalendarDataHandler(CalendarDataFormat.ICAL); calendarDataHandler.importCalendar(calendarId, data); jsonObject.put("success", true); } catch (Exception e) { String message = themeDisplay .translate("an-unexpected-error-occurred-while-importing-your-" + "file"); jsonObject.put("error", message); } } else { String message = themeDisplay.translate("failed-to-import-empty-file"); jsonObject.put("error", message); } writeJSON(resourceRequest, resourceResponse, jsonObject); }
From source file:com.liferay.calendar.web.internal.portlet.CalendarPortlet.java
License:Open Source License
public void importCalendar(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception { UploadPortletRequest uploadPortletRequest = _portal.getUploadPortletRequest(actionRequest); ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); long calendarId = ParamUtil.getLong(uploadPortletRequest, "calendarId"); File file = uploadPortletRequest.getFile("file"); String data = FileUtil.read(file); JSONObject jsonObject = JSONFactoryUtil.createJSONObject(); if (Validator.isNotNull(data)) { try {//from ww w . j a v a 2 s. com CalendarDataHandler calendarDataHandler = CalendarDataHandlerFactory .getCalendarDataHandler(CalendarDataFormat.ICAL); calendarDataHandler.importCalendar(calendarId, data); jsonObject.put("success", true); } catch (Exception e) { String message = themeDisplay.translate("an-unexpected-error-occurred-while-importing-your-file"); jsonObject.put("error", message); jsonObject.put("success", false); } } else { ResourceBundle resourceBundle = ResourceBundleUtil.getBundle("content.Language", themeDisplay.getLocale(), getClass()); String message = ResourceBundleUtil.getString(resourceBundle, "failed-to-import-empty-file"); jsonObject.put("error", message); jsonObject.put("success", false); } writeJSON(actionRequest, actionResponse, jsonObject); }
From source file:com.liferay.dynamic.data.lists.exporter.test.DDLExporterTest.java
License:Open Source License
@Test public void testCSVExport() throws Exception { DDMForm ddmForm = DDMFormTestUtil.createDDMForm(_availableLocales, _defaultLocale); createDDMFormFields(ddmForm);//from www . j ava 2 s . c om DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues(ddmForm, _availableLocales, _defaultLocale); createDDMFormFieldValues(ddmFormValues); DDLRecordSetTestHelper recordSetTestHelper = new DDLRecordSetTestHelper(_group); DDLRecordSet recordSet = recordSetTestHelper.addRecordSet(ddmForm); DDLRecordTestHelper recordTestHelper = new DDLRecordTestHelper(_group, recordSet); recordTestHelper.addRecord(ddmFormValues, WorkflowConstants.ACTION_PUBLISH); DDLExporter ddlExporter = _ddlExporterFactory.getDDLExporter("csv"); byte[] bytes = ddlExporter.export(recordSet.getRecordSetId()); File file = new File("record-set.csv"); FileUtil.write(file, bytes); String expectedFileContent = read("test-record-set-export.csv"); String actualFileContent = FileUtil.read(file); Assert.assertEquals(expectedFileContent, actualFileContent); }
From source file:com.liferay.dynamic.data.lists.exporter.test.DDLExporterTest.java
License:Open Source License
@Test public void testXMLExport() throws Exception { DDMForm ddmForm = DDMFormTestUtil.createDDMForm(_availableLocales, _defaultLocale); createDDMFormFields(ddmForm);/*from w w w. j a v a 2 s.com*/ DDMFormValues ddmFormValues = DDMFormValuesTestUtil.createDDMFormValues(ddmForm, _availableLocales, _defaultLocale); createDDMFormFieldValues(ddmFormValues); DDLRecordSetTestHelper recordSetTestHelper = new DDLRecordSetTestHelper(_group); DDLRecordSet recordSet = recordSetTestHelper.addRecordSet(ddmForm); DDLRecordTestHelper recordTestHelper = new DDLRecordTestHelper(_group, recordSet); recordTestHelper.addRecord(ddmFormValues, WorkflowConstants.ACTION_PUBLISH); DDLExporter ddlExporter = _ddlExporterFactory.getDDLExporter("xml"); byte[] bytes = ddlExporter.export(recordSet.getRecordSetId()); File file = new File("record-set.xml"); FileUtil.write(file, bytes); String expectedFileContent = read("test-record-set-export.xml"); String actualFileContent = FileUtil.read(file); Assert.assertEquals(expectedFileContent, actualFileContent); }
From source file:com.liferay.dynamic.data.mapping.web.internal.portlet.action.AddTemplateMVCActionCommand.java
License:Open Source License
protected String getFileScriptContent(UploadPortletRequest uploadPortletRequest) throws Exception { File file = uploadPortletRequest.getFile("script"); if (file == null) { return null; }//from ww w .j a va 2s. c om String fileScriptContent = FileUtil.read(file); String contentType = MimeTypesUtil.getContentType(file); if (Validator.isNotNull(fileScriptContent) && !isValidContentType(contentType)) { throw new TemplateScriptException("Invalid contentType " + contentType); } return fileScriptContent; }
From source file:com.liferay.opensocial.shindig.oauth.LiferayOAuthStoreProvider.java
License:Open Source License
private OAuthConsumer _getOAuthConsumer(String keyFileName, String keyName) { OAuthConsumer oAuthConsumer = new OAuthConsumerImpl(); oAuthConsumer.setConsumerKey(_DEFAULT_CONSUMER_KEY); oAuthConsumer.setServiceName(_DEFAULT_SERVICE_NAME); String consumerSecret = null; String path = PropsUtil.get(PropsKeys.LIFERAY_HOME).concat(_KEY_DIR); path = path.replaceAll(StringPool.QUOTE, StringPool.BLANK); keyFileName = path.concat(keyFileName); try {/*from w w w. j a v a2s . c o m*/ consumerSecret = FileUtil.read(keyFileName); } catch (Exception e) { } finally { if (consumerSecret == null) { if (!FileUtil.exists(path)) { FileUtil.mkdirs(path); } if (_log.isWarnEnabled()) { _log.warn("Unable to load OAuth key from " + keyFileName); } return null; } } consumerSecret = _convertFromOpenSsl(consumerSecret); oAuthConsumer.setConsumerSecret(consumerSecret); oAuthConsumer.setKeyType(OAuthConsumerConstants.KEY_TYPE_RSA_PRIVATE); oAuthConsumer.setKeyName(keyName); return oAuthConsumer; }
From source file:com.liferay.rtl.servlet.filters.dynamiccss.DynamicCSSFilter.java
License:Open Source License
protected Object getDynamicContent(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws Exception { String requestURI = request.getRequestURI(); String requestPath = requestURI; String contextPath = request.getContextPath(); if (!contextPath.equals(StringPool.SLASH)) { requestPath = requestPath.substring(contextPath.length()); }//w w w . j a va 2 s. c om URL resourceURL = _servletContext.getResource(requestPath); if (resourceURL == null) { return null; } URLConnection urlConnection = resourceURL.openConnection(); String cacheCommonFileName = getCacheFileName(request); File cacheContentTypeFile = new File(_tempDir, cacheCommonFileName + "_E_CONTENT_TYPE"); File cacheDataFile = new File(_tempDir, cacheCommonFileName + "_E_DATA"); if (cacheDataFile.exists() && (cacheDataFile.lastModified() >= urlConnection.getLastModified())) { if (cacheContentTypeFile.exists()) { String contentType = FileUtil.read(cacheContentTypeFile); response.setContentType(contentType); } return cacheDataFile; } String dynamicContent = null; String content = null; try { if (requestPath.endsWith(_CSS_EXTENSION)) { if (_log.isInfoEnabled()) { _log.info("Parsing SASS on CSS " + requestPath); } content = StringUtil.read(urlConnection.getInputStream()); dynamicContent = DynamicCSSUtil.parseSass(_servletContext, request, requestPath, content); response.setContentType(ContentTypes.TEXT_CSS); FileUtil.write(cacheContentTypeFile, ContentTypes.TEXT_CSS); } else if (requestPath.endsWith(_JSP_EXTENSION)) { if (_log.isInfoEnabled()) { _log.info("Parsing SASS on JSP or servlet " + requestPath); } BufferCacheServletResponse bufferCacheServletResponse = new BufferCacheServletResponse(response); processFilter(DynamicCSSFilter.class, request, bufferCacheServletResponse, filterChain); bufferCacheServletResponse.finishResponse(); content = bufferCacheServletResponse.getString(); dynamicContent = DynamicCSSUtil.parseSass(_servletContext, request, requestPath, content); FileUtil.write(cacheContentTypeFile, bufferCacheServletResponse.getContentType()); } else { return null; } } catch (Exception e) { _log.error("Unable to parse SASS on CSS " + requestPath, e); if (_log.isDebugEnabled()) { _log.debug(content); } response.setHeader(HttpHeaders.CACHE_CONTROL, HttpHeaders.CACHE_CONTROL_NO_CACHE_VALUE); } if (dynamicContent != null) { FileUtil.write(cacheDataFile, dynamicContent); } else { dynamicContent = content; } return dynamicContent; }
From source file:com.liferay.util.ant.Java2WsddTask.java
License:Open Source License
public static String[] generateWsdd(String className, String serviceName) throws Exception { // Create temp directory File tempDir = new File(Time.getTimestamp()); tempDir.mkdir();//from w ww . j a v a 2s . c o m // axis-java2wsdl String wsdlFileName = tempDir + "/service.wsdl"; int pos = className.lastIndexOf("."); String packagePath = className.substring(0, pos); String[] packagePaths = StringUtil.split(packagePath, '.'); String namespace = "urn:"; for (int i = packagePaths.length - 1; i >= 0; i--) { namespace += packagePaths[i]; if (i > 0) { namespace += "."; } } String location = "http://localhost/services/" + serviceName; String mappingPackage = packagePath.substring(0, packagePath.lastIndexOf(".")) + ".ws"; Project project = AntUtil.getProject(); Java2WsdlAntTask java2Wsdl = new Java2WsdlAntTask(); NamespaceMapping mapping = new NamespaceMapping(); mapping.setNamespace(namespace); mapping.setPackage(mappingPackage); java2Wsdl.setProject(project); java2Wsdl.setClassName(className); java2Wsdl.setOutput(new File(wsdlFileName)); java2Wsdl.setLocation(location); java2Wsdl.setNamespace(namespace); java2Wsdl.addMapping(mapping); java2Wsdl.execute(); // axis-wsdl2java Wsdl2javaAntTask wsdl2Java = new Wsdl2javaAntTask(); wsdl2Java.setProject(project); wsdl2Java.setURL(wsdlFileName); wsdl2Java.setOutput(tempDir); wsdl2Java.setServerSide(true); wsdl2Java.setTestCase(false); wsdl2Java.setVerbose(false); wsdl2Java.execute(); // Get content String deployContent = FileUtil .read(tempDir + "/" + StringUtil.replace(packagePath, ".", "/") + "/deploy.wsdd"); deployContent = StringUtil.replace(deployContent, packagePath + "." + serviceName + "SoapBindingImpl", className); deployContent = _format(deployContent); String undeployContent = FileUtil .read(tempDir + "/" + StringUtil.replace(packagePath, ".", "/") + "/undeploy.wsdd"); undeployContent = _format(undeployContent); // Delete temp directory DeleteTask.deleteDirectory(tempDir); return new String[] { deployContent, undeployContent }; }
From source file:com.liferay.util.xml.XMLMergerRunner.java
License:Open Source License
public void mergeAndSave(File masterFile, File slaveFile, File mergedFile) throws ClassNotFoundException, DocumentException, IllegalAccessException, InstantiationException, IOException { String xml1 = FileUtil.read(masterFile); String xml2 = FileUtil.read(slaveFile); String mergedXml = _merge(xml1, xml2); FileUtil.write(mergedFile, mergedXml); }