我遇到重定向问题,钩子没有指示添加消息。我到目前为止编写的代码是:
public class EditMessageCustomAction extends BaseStrutsPortletAction {
private static final String BB_CODE_EDITOR_WYSIWYG_IMPL_KEY = "editor.wysiwyg.portal-web.docroot.html.portlet.message_boards." +
"edit_message.bb_code.jsp";
public static final String[] BOOLEANS = {"true", "t", "y", "on", "1"};
@Override
public void processAction(
StrutsPortletAction originalStrutsPortletAction,
PortletConfig portletConfig, ActionRequest actionRequest,
ActionResponse actionResponse)
throws Exception {
_log.info("Hook processAction >>>>>>>>>>>>>>>>>>>>>>>>");
System.out.println("Hook processAction >>>>>>>>>>>>>>>>>>>>>>>>");
String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
MBMessage message = null;
try {
UploadException uploadException =
(UploadException)actionRequest.getAttribute(
WebKeys.UPLOAD_EXCEPTION);
if (uploadException != null) {
if (uploadException.isExceededLiferayFileItemSizeLimit()) {
throw new LiferayFileItemException();
}
else if (uploadException.isExceededSizeLimit()) {
throw new FileSizeException(uploadException.getCause());
}
throw new PortalException(uploadException.getCause());
}
else if (cmd.equals(Constants.ADD) ||
cmd.equals(Constants.UPDATE)) {
message = updateMessage(actionRequest, actionResponse);
}
else if (cmd.equals(Constants.DELETE)) {
deleteMessage(actionRequest);
}
else if (cmd.equals(Constants.LOCK)) {
lockThreads(actionRequest);
}
else if (cmd.equals(Constants.SUBSCRIBE)) {
subscribeMessage(actionRequest);
}
else if (cmd.equals(Constants.UNLOCK)) {
unlockThreads(actionRequest);
}
else if (cmd.equals(Constants.UNSUBSCRIBE)) {
unsubscribeMessage(actionRequest);
}
System.out.println("Message Posted + cmd " + cmd);
if (Validator.isNotNull(cmd)) {
String redirect = getRedirect(
actionRequest, actionResponse, message);
//originalStrutsPortletAction.processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
System.out.println("Before Send redi" + redirect);
sendRedirect( portletConfig, actionRequest, actionResponse,redirect,null);
}
}
catch (Exception e) {
e.printStackTrace();
if (e instanceof NoSuchMessageException ||
e instanceof PrincipalException ||
e instanceof RequiredMessageException) {
SessionErrors.add(actionRequest, e.getClass());
setForward(actionRequest, "portlet.message_boards.error");
}
else if (e instanceof CaptchaMaxChallengesException ||
e instanceof CaptchaTextException ||
e instanceof DuplicateFileException ||
e instanceof FileExtensionException ||
e instanceof FileNameException ||
e instanceof FileSizeException ||
e instanceof LiferayFileItemException ||
e instanceof LockedThreadException ||
e instanceof MessageBodyException ||
e instanceof MessageSubjectException ||
e instanceof SanitizerException) {
UploadException uploadException =
(UploadException)actionRequest.getAttribute(
WebKeys.UPLOAD_EXCEPTION);
if (uploadException != null) {
String uploadExceptionRedirect = ParamUtil.getString(
actionRequest, "uploadExceptionRedirect");
actionResponse.sendRedirect(uploadExceptionRedirect);
}
SessionErrors.add(actionRequest, e.getClass());
}
else if (e instanceof AssetCategoryException ||
e instanceof AssetTagException) {
SessionErrors.add(actionRequest, e.getClass(), e);
}
else {
Throwable cause = e.getCause();
if (cause instanceof SanitizerException) {
SessionErrors.add(actionRequest, SanitizerException.class);
}
else {
throw e;
}
}
}
}
protected boolean isDisplaySuccessMessage(PortletRequest portletRequest)
throws SystemException {
if (!SessionErrors.isEmpty(portletRequest)) {
return false;
}
ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
Layout layout = themeDisplay.getLayout();
if (layout.isTypeControlPanel()) {
return true;
}
String portletId = (String)portletRequest.getAttribute(
WebKeys.PORTLET_ID);
try {
LayoutTypePortlet layoutTypePortlet =
themeDisplay.getLayoutTypePortlet();
if (layoutTypePortlet.hasPortletId(portletId)) {
return true;
}
}
catch (PortalException pe) {
if (_log.isDebugEnabled()) {
_log.debug(pe, pe);
}
}
Portlet portlet = PortletLocalServiceUtil.getPortletById(
themeDisplay.getCompanyId(), portletId);
if (portlet.isAddDefaultResource()) {
return true;
}
return false;
}
protected void addSuccessMessage(
ActionRequest actionRequest, ActionResponse actionResponse) {
PortletConfig portletConfig = (PortletConfig)actionRequest.getAttribute(
JavaConstants.JAVAX_PORTLET_CONFIG);
boolean addProcessActionSuccessMessage = GetterUtil.getBoolean(
portletConfig.getInitParameter("add-process-action-success-action"),
true);
if (!addProcessActionSuccessMessage) {
return;
}
String successMessage = ParamUtil.getString(
actionRequest, "successMessage");
SessionMessages.add(actionRequest, "requestProcessed", successMessage);
}
protected void sendRedirect(
PortletConfig portletConfig, ActionRequest actionRequest,
ActionResponse actionResponse, String redirect,
String closeRedirect)
throws IOException, SystemException {
System.out.println("sendRedirect >>>>>>>>>>>>>>>>>>>>>>>>");
if (isDisplaySuccessMessage(actionRequest)) {
addSuccessMessage(actionRequest, actionResponse);
}
if (Validator.isNull(redirect)) {
System.out.println("sendRedirect 1 >>>>>>>>>>>>>>>>>>>>>>>>");
redirect = (String)actionRequest.getAttribute(WebKeys.REDIRECT);
System.out.println("sendRedirect 1 >>>>>>>>>>>>>>>>>>>>>>>>" + redirect);
}
if (Validator.isNull(redirect)) {
System.out.println("sendRedirect 2 >>>>>>>>>>>>>>>>>>>>>>>>");
redirect = ParamUtil.getString(actionRequest, "redirect");
System.out.println("sendRedirect 2 >>>>>>>>>>>>>>>>>>>>>>>>" + redirect);
}
if ((portletConfig != null) && Validator.isNotNull(redirect) &&
Validator.isNotNull(closeRedirect)) {
System.out.println("sendRedirect 3 >>>>>>>>>>>>>>>>>>>>>>>>");
redirect = HttpUtil.setParameter(
redirect, "closeRedirect", closeRedirect);
System.out.println("sendRedirect 4 >>>>>>>>>>>>>>>>>>>>>>>>" + redirect);
SessionMessages.add(
actionRequest,
PortalUtil.getPortletId(actionRequest) +
SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
closeRedirect);
System.out.println("sendRedirect 5 >>>>>>>>>>>>>>>>>>>>>>>>" + PortalUtil.getPortletId(actionRequest) +
SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT);
}
if (Validator.isNull(redirect)) {
System.out.println("sendRedirect final >>>>>>>>>>>>>>>>>>>>>>>>");
return;
}
// LPS-1928
HttpServletRequest request = PortalUtil.getHttpServletRequest(
actionRequest);
if (BrowserSnifferUtil.isIe(request) &&
(BrowserSnifferUtil.getMajorVersion(request) == 6.0) &&
redirect.contains(StringPool.POUND)) {
String redirectToken = "&#";
if (!redirect.contains(StringPool.QUESTION)) {
redirectToken = StringPool.QUESTION + redirectToken;
}
redirect = StringUtil.replace(
redirect, StringPool.POUND, redirectToken);
}
redirect = PortalUtil.escapeRedirect(redirect);
if (Validator.isNotNull(redirect)) {
actionResponse.sendRedirect(redirect);
}
}
@Override
public String render(StrutsPortletAction originalStrutsPortletAction,
PortletConfig portletConfig, RenderRequest renderRequest,
RenderResponse renderResponse) throws Exception {
return originalStrutsPortletAction.render(portletConfig, renderRequest, renderResponse);
}
protected String getForward(PortletRequest portletRequest) {
return getForward(portletRequest, null);
}
protected String getForward(
PortletRequest portletRequest, String defaultValue) {
String forward = (String)portletRequest.getAttribute(
getForwardKey(portletRequest));
if (forward == null) {
return defaultValue;
}
else {
return forward;
}
}
public static String getForwardKey(PortletRequest portletRequest) {
String portletId = (String)portletRequest.getAttribute(
WebKeys.PORTLET_ID);
String portletNamespace = PortalUtil.getPortletNamespace(portletId);
return portletNamespace.concat(WebKeys.PORTLET_STRUTS_FORWARD);
}
protected void setForward(PortletRequest portletRequest, String forward) {
portletRequest.setAttribute(getForwardKey(portletRequest), forward);
}
/*@Override
public void processAction(
ActionMapping actionMapping, ActionForm actionForm,
PortletConfig portletConfig, ActionRequest actionRequest,
ActionResponse actionResponse)
throws Exception {
String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
MBMessage message = null;
System.out.println("processAction ***********************************************************");
try {
UploadException uploadException =
(UploadException)actionRequest.getAttribute(
WebKeys.UPLOAD_EXCEPTION);
if (uploadException != null) {
if (uploadException.isExceededLiferayFileItemSizeLimit()) {
throw new LiferayFileItemException();
}
else if (uploadException.isExceededSizeLimit()) {
throw new FileSizeException(uploadException.getCause());
}
throw new PortalException(uploadException.getCause());
}
else if (cmd.equals(Constants.ADD) ||
cmd.equals(Constants.UPDATE)) {
message = updateMessage(actionRequest, actionResponse);
}
else if (cmd.equals(Constants.DELETE)) {
deleteMessage(actionRequest);
}
else if (cmd.equals(Constants.LOCK)) {
lockThreads(actionRequest);
}
else if (cmd.equals(Constants.SUBSCRIBE)) {
subscribeMessage(actionRequest);
}
else if (cmd.equals(Constants.UNLOCK)) {
unlockThreads(actionRequest);
}
else if (cmd.equals(Constants.UNSUBSCRIBE)) {
unsubscribeMessage(actionRequest);
}
if (Validator.isNotNull(cmd)) {
String redirect = getRedirect(
actionRequest, actionResponse, message);
sendRedirect(actionRequest, actionResponse, redirect);
}
}
catch (Exception e) {
if (e instanceof NoSuchMessageException ||
e instanceof PrincipalException ||
e instanceof RequiredMessageException) {
SessionErrors.add(actionRequest, e.getClass());
setForward(actionRequest, "portlet.message_boards.error");
}
else if (e instanceof CaptchaMaxChallengesException ||
e instanceof CaptchaTextException ||
e instanceof DuplicateFileException ||
e instanceof FileExtensionException ||
e instanceof FileNameException ||
e instanceof FileSizeException ||
e instanceof LiferayFileItemException ||
e instanceof LockedThreadException ||
e instanceof MessageBodyException ||
e instanceof MessageSubjectException ||
e instanceof SanitizerException) {
UploadException uploadException =
(UploadException)actionRequest.getAttribute(
WebKeys.UPLOAD_EXCEPTION);
if (uploadException != null) {
String uploadExceptionRedirect = ParamUtil.getString(
actionRequest, "uploadExceptionRedirect");
actionResponse.sendRedirect(uploadExceptionRedirect);
}
SessionErrors.add(actionRequest, e.getClass());
}
else if (e instanceof AssetCategoryException ||
e instanceof AssetTagException) {
SessionErrors.add(actionRequest, e.getClass(), e);
}
else {
Throwable cause = e.getCause();
if (cause instanceof SanitizerException) {
SessionErrors.add(actionRequest, SanitizerException.class);
}
else {
throw e;
}
}
}
}
@Override
public ActionForward render(
ActionMapping actionMapping, ActionForm actionForm,
PortletConfig portletConfig, RenderRequest renderRequest,
RenderResponse renderResponse)
throws Exception {
System.out.println("Render *****************************************");
try {
ActionUtil.getMessage(renderRequest);
}
catch (Exception e) {
if (e instanceof NoSuchMessageException ||
e instanceof PrincipalException) {
SessionErrors.add(renderRequest, e.getClass());
return actionMapping.findForward(
"portlet.message_boards.error");
}
else {
throw e;
}
}
return actionMapping.findForward(
getForward(renderRequest, "portlet.message_boards.edit_message"));
}
*/
protected void deleteMessage(ActionRequest actionRequest) throws Exception {
long messageId = ParamUtil.getLong(actionRequest, "messageId");
MBMessageServiceUtil.deleteMessage(messageId);
}
protected String getRedirect(
ActionRequest actionRequest, ActionResponse actionResponse,
MBMessage message) {
if (message == null) {
String redirect = ParamUtil.getString(actionRequest, "redirect");
return redirect;
}
int workflowAction = ParamUtil.getInteger(
actionRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);
if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
return getSaveAndContinueRedirect(
actionRequest, actionResponse, message);
}
else if (message == null) {
return ParamUtil.getString(actionRequest, "redirect");
}
ActionResponseImpl actionResponseImpl =
(ActionResponseImpl)actionResponse;
PortletURL portletURL = actionResponseImpl.createRenderURL();
portletURL.setParameter(
"struts_action", "/message_boards/view_message");
portletURL.setParameter(
"messageId", String.valueOf(message.getMessageId()));
return portletURL.toString();
}
protected String getSaveAndContinueRedirect(
ActionRequest actionRequest, ActionResponse actionResponse,
MBMessage message) {
String redirect = ParamUtil.getString(actionRequest, "redirect");
boolean preview = ParamUtil.getBoolean(actionRequest, "preview");
PortletURL portletURL =
((ActionResponseImpl)actionResponse).createRenderURL();
portletURL.setParameter(
"struts_action", "/message_boards/edit_message");
portletURL.setParameter("redirect", redirect);
portletURL.setParameter(
"messageId", String.valueOf(message.getMessageId()));
portletURL.setParameter("preview", String.valueOf(preview));
return portletURL.toString();
}
protected void lockThreads(ActionRequest actionRequest) throws Exception {
long threadId = ParamUtil.getLong(actionRequest, "threadId");
if (threadId > 0) {
MBThreadServiceUtil.lockThread(threadId);
}
else {
long[] threadIds = StringUtil.split(
ParamUtil.getString(actionRequest, "threadIds"), 0L);
for (int i = 0; i < threadIds.length; i++) {
MBThreadServiceUtil.lockThread(threadIds[i]);
}
}
}
protected void subscribeMessage(ActionRequest actionRequest)
throws Exception {
long messageId = ParamUtil.getLong(actionRequest, "messageId");
MBMessageServiceUtil.subscribeMessage(messageId);
}
protected void unlockThreads(ActionRequest actionRequest) throws Exception {
long threadId = ParamUtil.getLong(actionRequest, "threadId");
if (threadId > 0) {
MBThreadServiceUtil.unlockThread(threadId);
}
else {
long[] threadIds = StringUtil.split(
ParamUtil.getString(actionRequest, "threadIds"), 0L);
for (int i = 0; i < threadIds.length; i++) {
MBThreadServiceUtil.unlockThread(threadIds[i]);
}
}
}
protected void unsubscribeMessage(ActionRequest actionRequest)
throws Exception {
long messageId = ParamUtil.getLong(actionRequest, "messageId");
MBMessageServiceUtil.unsubscribeMessage(messageId);
}
protected MBMessage updateMessage(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
System.out.println("updateMessage *****************************************");
PortletPreferences portletPreferences = actionRequest.getPreferences();
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
WebKeys.THEME_DISPLAY);
long messageId = ParamUtil.getLong(actionRequest, "messageId");
long groupId = themeDisplay.getScopeGroupId();
long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
long threadId = ParamUtil.getLong(actionRequest, "threadId");
long parentMessageId = ParamUtil.getLong(
actionRequest, "parentMessageId");
String subject = ParamUtil.getString(actionRequest, "subject") + "<<<Indu>>>" ;
String body = ParamUtil.getString(actionRequest, "body");
String format = GetterUtil.getString(
portletPreferences.getValue("messageFormat", null),
MBMessageConstants.DEFAULT_FORMAT);
//if (!isValidMessageFormat(format)) {
format = "html";
//}
List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
new ArrayList<ObjectValuePair<String, InputStream>>(5);
try {
UploadPortletRequest uploadPortletRequest =
PortalUtil.getUploadPortletRequest(actionRequest);
for (int i = 1; i <= 5; i++) {
String fileName = uploadPortletRequest.getFileName(
"msgFile" + i);
InputStream inputStream = uploadPortletRequest.getFileAsStream(
"msgFile" + i);
if ((inputStream == null) || Validator.isNull(fileName)) {
continue;
}
ObjectValuePair<String, InputStream> inputStreamOVP =
new ObjectValuePair<String, InputStream>(
fileName, inputStream);
inputStreamOVPs.add(inputStreamOVP);
}
boolean question = ParamUtil.getBoolean(actionRequest, "question");
boolean anonymous = ParamUtil.getBoolean(
actionRequest, "anonymous");
double priority = ParamUtil.getDouble(actionRequest, "priority");
boolean allowPingbacks = ParamUtil.getBoolean(
actionRequest, "allowPingbacks");
ServiceContext serviceContext = ServiceContextFactory.getInstance(
MBMessage.class.getName(), actionRequest);
boolean preview = ParamUtil.getBoolean(actionRequest, "preview");
serviceContext.setAttribute("preview", preview);
MBMessage message = null;
if (messageId <= 0) {
if (getBoolean(
PropsUtil.get(PropsKeys.CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_MESSAGE),false) ) {
CaptchaUtil.check(actionRequest);
}
if (threadId <= 0) {
// Post new thread
message = MBMessageServiceUtil.addMessage(
groupId, categoryId, subject, body, format,
inputStreamOVPs, anonymous, priority, allowPingbacks,
serviceContext);
if (question) {
MBThreadLocalServiceUtil.updateQuestion(
message.getThreadId(), true);
}
}
else {
// Post reply
message = MBMessageServiceUtil.addMessage(
parentMessageId, subject, body, format, inputStreamOVPs,
anonymous, priority, allowPingbacks, serviceContext);
}
}
else {
List<String> existingFiles = new ArrayList<String>();
for (int i = 1; i <= 5; i++) {
String path = ParamUtil.getString(
actionRequest, "existingPath" + i);
if (Validator.isNotNull(path)) {
existingFiles.add(path);
}
}
// Update message
message = MBMessageServiceUtil.updateMessage(
messageId, subject, body, inputStreamOVPs, existingFiles,
priority, allowPingbacks, serviceContext);
if (message.isRoot()) {
MBThreadLocalServiceUtil.updateQuestion(
message.getThreadId(), question);
}
}
PermissionChecker permissionChecker =
themeDisplay.getPermissionChecker();
boolean subscribe = ParamUtil.getBoolean(
actionRequest, "subscribe");
/*if (!preview && subscribe &&
MBMessagePermission.contains(
permissionChecker, message, ActionKeys.SUBSCRIBE)) {*/
MBMessageServiceUtil.subscribeMessage(message.getMessageId());
/*}*/
return message;
}
finally {
for (ObjectValuePair<String, InputStream> inputStreamOVP :
inputStreamOVPs) {
InputStream inputStream = inputStreamOVP.getValue();
StreamUtil.cleanUp(inputStream);
}
}
}
private boolean getBoolean(String value, boolean defaultValue) {
if (value == null) {
return defaultValue;
}
value = value.trim();
value = StringUtil.toLowerCase(value);
if (value.equals(BOOLEANS[0]) || value.equals(BOOLEANS[1]) ||
value.equals(BOOLEANS[2]) || value.equals(BOOLEANS[3]) ||
value.equals(BOOLEANS[4])) {
return true;
}
else {
return false;
}
}
public static boolean isValidMessageFormat(String messageFormat) {
String editorImpl = PropsUtil.get(BB_CODE_EDITOR_WYSIWYG_IMPL_KEY);
if (messageFormat.equals("bbcode") &&
!(editorImpl.equals("bbcode") ||
editorImpl.equals("ckeditor_bbcode"))) {
return false;
}
return true;
}
private static Log _log = LogFactoryUtil.getLog(EditMessageCustomAction.class);
}
需要门户服务器的一些核心对象,它们打包在portal-impl
中,不能作为钩子库中的依赖项添加。请提出一个好的做法。
答案 0 :(得分:1)
看起来你已经将大量Liferay的代码复制到了你的钩子中。当你要求最佳实践:这不是它。您似乎创建了一个Struts Action Hook,它通常只需要一个方法,您可以在委托之前或之后执行任何您想要的任何方法到originalStrutsAction。
如果您复制Liferay的实现,它自然可能会引用不属于API的类 - 您需要自己实现该功能。但是,Struts Action通常用于为已经发生的事件添加一些额外的功能。你没有说明你真正想要实现的目标(除了编写一个钩子)而且我不会破译你发布的代码量 - 更不用说试试看你想要的了什么做。
最佳实践:实施您的添加,然后委托originalStrutsAction。或者相反。不要复制Liferay的实施。并且,无论如何:无论发生什么,不要将portal-impl.jar添加到插件的依赖项中