protected Layout importLayoutFromLAR(StagedModel stagedModel) throws DocumentException, IOException { LayoutSetPrototypeStagedModelDataHandler layoutSetPrototypeStagedModelDataHandler = new LayoutSetPrototypeStagedModelDataHandler(); String fileName = layoutSetPrototypeStagedModelDataHandler.getLayoutSetPrototypeLARFileName( (LayoutSetPrototype) stagedModel); String modelPath = ExportImportPathUtil.getModelPath(stagedModel, fileName); InputStream inputStream = portletDataContext.getZipEntryAsInputStream(modelPath); ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(inputStream); Document document = UnsecureSAXReaderUtil.read(zipReader.getEntryAsString("manifest.xml")); Element rootElement = document.getRootElement(); Element layoutElement = rootElement.element("Layout"); List<Element> elements = layoutElement.elements(); List<Layout> importedLayouts = new ArrayList<>(elements.size()); for (Element element : elements) { String layoutPrototypeUuid = element.attributeValue("layout-prototype-uuid"); if (Validator.isNotNull(layoutPrototypeUuid)) { String path = element.attributeValue("path"); Layout layout = (Layout) portletDataContext.fromXML(zipReader.getEntryAsString(path)); importedLayouts.add(layout); } } Assert.assertEquals(1, importedLayouts.size()); try { return importedLayouts.get(0); } finally { zipReader.close(); StreamUtil.cleanUp(inputStream); } }
protected void processKBArticleFiles( long userId, long groupId, ZipReader zipReader, ServiceContext serviceContext) throws KBArticleImportException { long parentResourcePrimKey = KBArticleConstants.DEFAULT_PARENT_RESOURCE_PRIM_KEY; String markdown = zipReader.getEntryAsString(PortletPropsValues.MARKDOWN_IMPORTER_ARTICLE_HOME); if (Validator.isNotNull(markdown)) { KBArticle parentKBArticle = addKBArticleMarkdown( userId, groupId, KBArticleConstants.DEFAULT_PARENT_RESOURCE_PRIM_KEY, markdown, PortletPropsValues.MARKDOWN_IMPORTER_ARTICLE_HOME, zipReader, serviceContext); parentResourcePrimKey = parentKBArticle.getResourcePrimKey(); } processSectionKBArticleFiles( userId, groupId, parentResourcePrimKey, zipReader, getFolderNameFileEntryNamesMap(zipReader), serviceContext); }
@Override @Transactional(enabled = false) public Map<String, Object> updateFileEntries(File zipFile) throws PortalException { Map<String, Object> responseMap = new HashMap<>(); ZipReader zipReader = null; try { zipReader = ZipReaderFactoryUtil.getZipReader(zipFile); String manifest = zipReader.getEntryAsString("/manifest.json"); JSONArray jsonArray = JSONFactoryUtil.createJSONArray(manifest); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); JSONWebServiceActionParametersMap jsonWebServiceActionParametersMap = JSONFactoryUtil.looseDeserialize( jsonObject.toString(), JSONWebServiceActionParametersMap.class); String zipFileId = MapUtil.getString(jsonWebServiceActionParametersMap, "zipFileId"); try { responseMap.put( zipFileId, updateFileEntries(zipReader, zipFileId, jsonWebServiceActionParametersMap)); } catch (Exception e) { String message = e.getMessage(); if (!message.startsWith(StringPool.QUOTE) && !message.endsWith(StringPool.QUOTE)) { message = StringUtil.quote(message, StringPool.QUOTE); } String json = "{\"exception\": " + message + "}"; responseMap.put(zipFileId, json); } } } finally { if (zipReader != null) { zipReader.close(); } } return responseMap; }
protected void validateFile(long companyId, long groupId, String portletId, ZipReader zipReader) throws Exception { // XML String xml = zipReader.getEntryAsString("/manifest.xml"); if (xml == null) { throw new LARFileException("manifest.xml not found in the LAR"); } Element rootElement = null; try { Document document = SAXReaderUtil.read(xml); rootElement = document.getRootElement(); } catch (Exception e) { throw new LARFileException(e); } // Build compatibility int buildNumber = ReleaseInfo.getBuildNumber(); Element headerElement = rootElement.element("header"); int importBuildNumber = GetterUtil.getInteger(headerElement.attributeValue("build-number")); if (buildNumber != importBuildNumber) { throw new LayoutImportException( "LAR build number " + importBuildNumber + " does not match " + "portal build number " + buildNumber); } // Type String larType = headerElement.attributeValue("type"); if (!larType.equals("portlet")) { throw new LARTypeException(larType); } // Portlet compatibility String rootPortletId = headerElement.attributeValue("root-portlet-id"); if (!PortletConstants.getRootPortletId(portletId).equals(rootPortletId)) { throw new PortletIdException("Invalid portlet id " + rootPortletId); } // Available locales Portlet portlet = _portletLocalService.getPortletById(companyId, portletId); PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance(); if (portletDataHandler.isDataLocalized()) { List<Locale> sourceAvailableLocales = Arrays.asList( LocaleUtil.fromLanguageIds( StringUtil.split(headerElement.attributeValue("available-locales")))); for (Locale sourceAvailableLocale : sourceAvailableLocales) { if (!LanguageUtil.isAvailableLocale( PortalUtil.getSiteGroupId(groupId), sourceAvailableLocale)) { LocaleException le = new LocaleException( LocaleException.TYPE_EXPORT_IMPORT, "Locale " + sourceAvailableLocale + " is not " + "available in company " + companyId); le.setSourceAvailableLocales(sourceAvailableLocales); le.setTargetAvailableLocales( LanguageUtil.getAvailableLocales(PortalUtil.getSiteGroupId(groupId))); throw le; } } } }
protected void processSectionKBArticleFiles( long userId, long groupId, long parentResourcePrimaryKey, ZipReader zipReader, Map<String, List<String>> folderNameFileEntryNamesMap, Map<String, String> metadata, ServiceContext serviceContext) throws KBArticleImportException { Set<String> folderNames = folderNameFileEntryNamesMap.keySet(); for (String folderName : folderNames) { List<String> fileEntryNames = folderNameFileEntryNamesMap.get(folderName); String sectionIntroFileEntryName = null; List<String> sectionFileEntryNames = new ArrayList<String>(); for (String fileEntryName : fileEntryNames) { if (fileEntryName.endsWith(PortletPropsValues.MARKDOWN_IMPORTER_ARTICLE_INTRO)) { sectionIntroFileEntryName = fileEntryName; } else { sectionFileEntryNames.add(fileEntryName); } } if (Validator.isNull(sectionIntroFileEntryName) && !sectionFileEntryNames.isEmpty()) { StringBundler sb = new StringBundler(4); sb.append("No file entry ending in "); sb.append(PortletPropsValues.MARKDOWN_IMPORTER_ARTICLE_INTRO); sb.append(" accompanies file entry "); sb.append(sectionFileEntryNames.get(0)); throw new KBArticleImportException(sb.toString()); } KBArticle sectionIntroKBArticle = addKBArticleMarkdown( userId, groupId, parentResourcePrimaryKey, zipReader.getEntryAsString(sectionIntroFileEntryName), sectionIntroFileEntryName, zipReader, metadata, serviceContext); for (String sectionFileEntryName : sectionFileEntryNames) { String sectionMarkdown = zipReader.getEntryAsString(sectionFileEntryName); if (Validator.isNull(sectionMarkdown)) { if (_log.isWarnEnabled()) { _log.warn("Missing Markdown in file entry " + sectionFileEntryName); } } addKBArticleMarkdown( userId, groupId, sectionIntroKBArticle.getResourcePrimKey(), sectionMarkdown, sectionFileEntryName, zipReader, metadata, serviceContext); } } }
protected String importTheme(LayoutSet layoutSet, InputStream themeZip) throws Exception { ThemeLoader themeLoader = ThemeLoaderFactory.getDefaultThemeLoader(); if (themeLoader == null) { _log.error("No theme loaders are deployed"); return null; } ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(themeZip); String lookAndFeelXML = zipReader.getEntryAsString("liferay-look-and-feel.xml"); String themeId = String.valueOf(layoutSet.getGroupId()); if (layoutSet.isPrivateLayout()) { themeId += "-private"; } else { themeId += "-public"; } if (PropsValues.THEME_LOADER_NEW_THEME_ID_ON_IMPORT) { Date now = new Date(); themeId += "-" + Time.getShortTimestamp(now); } String themeName = themeId; lookAndFeelXML = StringUtil.replace( lookAndFeelXML, new String[] {"[$GROUP_ID$]", "[$THEME_ID$]", "[$THEME_NAME$]"}, new String[] {String.valueOf(layoutSet.getGroupId()), themeId, themeName}); FileUtil.deltree(themeLoader.getFileStorage() + StringPool.SLASH + themeId); List<String> zipEntries = zipReader.getEntries(); for (String zipEntry : zipEntries) { String key = zipEntry; if (key.equals("liferay-look-and-feel.xml")) { FileUtil.write( themeLoader.getFileStorage() + StringPool.SLASH + themeId + StringPool.SLASH + key, lookAndFeelXML.getBytes()); } else { InputStream is = zipReader.getEntryAsInputStream(zipEntry); FileUtil.write( themeLoader.getFileStorage() + StringPool.SLASH + themeId + StringPool.SLASH + key, is); } } themeLoader.loadThemes(); ClusterRequest clusterRequest = ClusterRequest.createMulticastRequest(_loadThemesMethodHandler, true); clusterRequest.setFireAndForget(true); ClusterExecutorUtil.execute(clusterRequest); themeId += PortletConstants.WAR_SEPARATOR + themeLoader.getServletContextName(); return PortalUtil.getJsSafePortletId(themeId); }