1 package com.sri.emo.controller; 2 3 import com.jcorporate.expresso.core.controller.*; 4 import com.sri.common.controller.AbstractComponentController; 5 import com.sri.common.controller.ControllerUtil; 6 import com.sri.emo.EmoSchema; 7 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 import java.util.List; 11 12 /*** 13 * Intermediate Controller Class that provides services common to editing 14 * nodes. Examples are embedding, etc. 15 * 16 * @author Michael Rimov 17 * @version 1.0 18 */ 19 public class NodeController extends AbstractComponentController { 20 21 /*** 22 * 23 */ 24 private static final long serialVersionUID = 1L; 25 26 27 /*** 28 * Default constructor. 29 */ 30 public NodeController() { 31 super(EmoSchema.class); 32 } 33 34 public NodeController(Class schemaClass) { 35 super(schemaClass); 36 } 37 38 /*** 39 * Location of the return URL. 40 */ 41 public static final String PARAM_RETURN_URL = "ReturnUrl"; 42 43 44 /*** 45 * Adds the return to sender as a parameter in the response 46 * 47 * @param request ControllerRequest 48 * @param response ControllerResponse 49 * @param transToModify The Transition that is going to get the new parameter. 50 * @throws ControllerException upon error populating the transition. 51 */ 52 protected void addReturnToSenderParameter(final ExpressoRequest request, 53 final ExpressoResponse response, 54 final Transition transToModify) throws ControllerException { 55 Transition reflexiveTransition = ControllerUtil.generateReflextiveTransition(request.getAllParameters(), 56 this.getClass(), response.getRequestedState()); 57 reflexiveTransition.setControllerResponse(response); 58 transToModify.addParam(PARAM_RETURN_URL, reflexiveTransition.getFullUrl()); 59 } 60 61 62 /*** 63 * Sets any previously set 'return to sender' URLS if they have been set. 64 * 65 * @param request ControllerRequest 66 * @param response ControllerResponse 67 * @param transToModify Transition 68 * @return boolean true if we found a previous Return to sender url. 69 * @throws ControllerException 70 */ 71 protected boolean propagateReturnToSenderParameter(final ExpressoRequest request, 72 final ExpressoResponse response, 73 final Transition transToModify) throws ControllerException { 74 if (request.getParameter(PARAM_RETURN_URL) == null) { 75 return false; 76 } else { 77 transToModify.addParam(PARAM_RETURN_URL, request.getParameter(PARAM_RETURN_URL)); 78 return true; 79 } 80 } 81 82 /*** 83 * Detects if we are running in embedded mode and redirects to the sender based on 84 * the return url (if supplied). 85 * 86 * @param request ServletControllerRequest 87 * @param message The Message to give to the user to acknowledge the save/update, etc. 88 * @return boolean if we do redirect to sender. Otherwise, continue on 89 * and do whatever you were going to do normally. 90 * @throws ControllerException 91 */ 92 protected boolean redirectToSender(final ServletControllerRequest request, final String message) throws 93 ControllerException { 94 if (isEmbeddedMode(request)) { 95 boolean sentRedirect = false; 96 if (message != null) { 97 this.addWarning(request, message); 98 } 99 100 if (request.getParameter(PARAM_RETURN_URL) != null) { 101 String redirectUrl = request.getParameter(PARAM_RETURN_URL); 102 HttpServletResponse servletResponse = (HttpServletResponse) request.getServletResponse(); 103 try { 104 sentRedirect = true; 105 List warningList = this.getWarningList(request); 106 request.getSession().removeAttribute(WARNING_LIST); 107 request.getSession().setPersistentAttribute(WARNING_LIST, warningList); 108 servletResponse.sendRedirect(servletResponse.encodeRedirectURL(redirectUrl)); 109 } catch (IOException ex) { 110 throw new ControllerException("IO Error sending redirect", ex); 111 } 112 } 113 return sentRedirect; 114 } else { 115 return false; 116 } 117 } 118 }