Java tutorial
/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.platts.portlet.documentlibrary; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.log.Log; import com.liferay.portal.kernel.log.LogFactoryUtil; import com.liferay.portal.kernel.repository.model.FileEntry; import com.liferay.portal.kernel.servlet.SessionErrors; import com.liferay.portal.kernel.servlet.SessionMessages; import com.liferay.portal.kernel.struts.BaseStrutsPortletAction; import com.liferay.portal.kernel.struts.StrutsPortletAction; import com.liferay.portal.kernel.util.ParamUtil; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.kernel.xml.Document; import com.liferay.portal.kernel.xml.DocumentException; import com.liferay.portal.kernel.xml.SAXReaderUtil; import com.liferay.portal.theme.ThemeDisplay; import com.liferay.portal.util.PortalUtil; import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil; import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil; import com.platts.export.util.PlattsExportUtil; import java.io.File; import java.io.IOException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletConfig; import javax.portlet.PortletRequest; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import javax.portlet.ResourceRequest; import javax.portlet.ResourceResponse; import org.apache.http.HttpEntity; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.FileEntity; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; public class DLSetSeoUrl extends BaseStrutsPortletAction { @Override public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws PortalException, SystemException, IOException { LOGGER.info("the process action for struts action DLSetSeoUrl is getting called"); String uri = ParamUtil.getString(actionRequest, "uri", null); String seoURL = ParamUtil.getString(actionRequest, "seoURL", null); Long fileEntryId = ParamUtil.getLong(actionRequest, "fileEntryId"); LOGGER.info("uri is " + uri + " seourl is " + seoURL + " fileEntryId " + fileEntryId); FileEntry fileEntry = DLAppLocalServiceUtil.getFileEntry(fileEntryId); // sample url http://10.206.111.1:8030/v1/resources/content?uri=/test/holiday.xml&seoUrl=/EditorBios/regina1233johnson.xml StringBuilder url = new StringBuilder("http://10.206.111.1:8030/v1/resources/content?uri="); url.append(uri).append(StringPool.SLASH).append(fileEntry.getTitle()).append("&seoUrl=").append(seoURL); LOGGER.info("the url is " + url.toString()); PlattsExportUtil exportUtil = new PlattsExportUtil(); ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); File file = exportUtil.getFileFromDLFile(themeDisplay.getUserId(), fileEntry.getFileEntryId(), fileEntry.getLatestFileVersion().getVersion()); //LOGGER.info("the file is " + FileUtils.readFileToString(file)); FileEntity fileEntity = new FileEntity(file); fileEntity.setContentType("application/xml"); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("CPUser", "CPUser22"); credentialsProvider.setCredentials(AuthScope.ANY, credentials); CloseableHttpClient httpClient = HttpClientBuilder.create() .setDefaultCredentialsProvider(credentialsProvider).build(); HttpPost postRequest = new HttpPost(url.toString()); postRequest.setEntity(fileEntity); try { CloseableHttpResponse response = httpClient.execute(postRequest); HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity); Document document = SAXReaderUtil.read(responseString); String responseText = document.getRootElement().selectSingleNode("/response").getText(); if (responseText.equalsIgnoreCase("ok")) { SessionMessages.add(actionRequest, "seoURLActionSuccess"); hideDefaultSuccessMessage(actionRequest); String redirect = PortalUtil.escapeRedirect(ParamUtil.getString(actionRequest, "redirect")); try { actionResponse.sendRedirect(redirect); } catch (IOException e) { LOGGER.error("error redirecting ", e); } } else { SessionErrors.add(actionRequest, "seoURLActionFailure"); LOGGER.info("the error text is " + responseText); actionResponse.setRenderParameter("errorMessage", responseText); } response.close(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } protected void hideDefaultSuccessMessage(PortletRequest portletRequest) { SessionMessages.add(portletRequest, PortalUtil.getPortletId(portletRequest) + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE); } @Override public String render(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception { String fileEntryId = renderRequest.getParameter("fileEntryId"); String viewJsp = renderRequest.getParameter("view-jsp"); if (Validator.isNull(viewJsp)) { viewJsp = "/portlet/document_library/set_seo_url.jsp"; } FileEntry fileEntry = DLAppServiceUtil.getFileEntry(Long.valueOf(fileEntryId)); PlattsExportUtil exportUtil = new PlattsExportUtil(); String filePath = exportUtil.getFullPathOfFileEntry(fileEntry.getFolderId()); LOGGER.info("the file path is " + filePath); renderRequest.setAttribute("errorMessage", renderRequest.getParameter("errorMessage")); renderRequest.setAttribute("folderPathforSEO", filePath); renderRequest.setAttribute("DOCUMENT_LIBRARY_FILE_ENTRY", fileEntry); renderRequest.setAttribute("fileEntryId", fileEntryId); String redirectURL = renderRequest.getParameter("redirect"); redirectURL = redirectURL.replaceAll("p_p_lifecycle=2", "p_p_lifecycle=0"); LOGGER.info("the redirect url is " + redirectURL); renderRequest.setAttribute("redirect", redirectURL); renderRequest.setAttribute("struts_action", "/document_library/set_seo_url"); renderRequest.setAttribute("cmd", "setSEOUrl"); renderRequest.setAttribute("folderId", fileEntry.getFolderId()); return viewJsp; } @Override public void serveResource(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws Exception { originalStrutsPortletAction.serveResource(originalStrutsPortletAction, portletConfig, resourceRequest, resourceResponse); } private static Log LOGGER = LogFactoryUtil.getLog(DLSetSeoUrl.class); }