use of javax.faces.context.ResponseWriter in project acs-community-packaging by Alfresco.
the class InvokeCommand method execute.
// ///////////////////////////////////////////////////////////////////////////
public void execute(final FacesContext facesContext, final String expression, final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
ResponseWriter writer = null;
try {
final int indexOfDot = expression.indexOf('.');
final String variableName = expression.substring(0, indexOfDot);
final String methodName = expression.substring(indexOfDot + 1);
if (logger.isDebugEnabled())
logger.debug("Invoking method represented by " + expression + " on variable " + variableName + " with method " + methodName);
Object bean = null;
if (Application.inPortalServer()) {
// retrieve the managed bean, this is really weak but if the
// request comes from a portal server the bean we need to get
// is in the session with a prefix chosen by the portal vendor,
// to cover this scenario we have to go through the names of
// all the objects in the session to find the bean we want.
String beanNameSuffix = "?" + variableName;
Enumeration<?> enumNames = request.getSession().getAttributeNames();
while (enumNames.hasMoreElements()) {
String name = (String) enumNames.nextElement();
if (name.endsWith(beanNameSuffix)) {
bean = request.getSession().getAttribute(name);
if (logger.isDebugEnabled())
logger.debug("Found bean " + bean + " in the session");
break;
}
}
}
// if we don't have the bean yet try and get it via the variable resolver
if (bean == null) {
VariableResolver vr = facesContext.getApplication().getVariableResolver();
bean = vr.resolveVariable(facesContext, variableName);
if (logger.isDebugEnabled())
logger.debug("Created bean " + bean + " via the variable resolver");
}
final Method method = bean.getClass().getMethod(methodName);
final String responseMimetype = (method.isAnnotationPresent(ResponseMimetype.class) ? method.getAnnotation(ResponseMimetype.class).value() : MimetypeMap.MIMETYPE_XML);
if (logger.isDebugEnabled())
logger.debug("invoking method " + method + " with repsonse mimetype " + responseMimetype);
writer = this.setupResponseWriter(responseMimetype, response, facesContext);
// setup the transaction
RetryingTransactionHelper txnHelper = Repository.getRetryingTransactionHelper(FacesContext.getCurrentInstance());
final Object beanFinal = bean;
RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {
public Object execute() throws Throwable {
// invoke the method
try {
method.invoke(beanFinal);
return null;
}// Let's prevent RuntimeExceptions being wrapped twice by unwrapping InvocationTargetExceptions
catch (InvocationTargetException e) {
if (e.getCause() != null) {
throw e.getCause();
}
throw e;
}
}
};
txnHelper.doInTransaction(callback);
} catch (EvaluationException e) {
Throwable err = e.getCause();
if (err == null) {
logger.error("Failed to execute method " + expression + ": " + e.getMessage(), e);
throw e;
} else {
logger.error("Failed to execute method " + expression + ": " + err.getMessage(), err);
if (err instanceof RuntimeException) {
throw (RuntimeException) err;
} else {
throw new AlfrescoRuntimeException("Failed to execute method " + expression + ": " + err.getMessage(), err);
}
}
} catch (RuntimeException err) {
logger.error("Failed to execute method " + expression + ": " + err.getMessage(), err);
throw err;
} catch (Exception err) {
logger.error("Failed to execute method " + expression + ": " + err.getMessage(), err);
throw new AlfrescoRuntimeException("Failed to execute method " + expression + ": " + err.getMessage(), err);
}
// force the output back to the client
writer.close();
}
use of javax.faces.context.ResponseWriter in project acs-community-packaging by Alfresco.
the class BaseAssociationEditor method encodeBegin.
/**
* @see javax.faces.component.UIComponent#encodeBegin(javax.faces.context.FacesContext)
*/
public void encodeBegin(FacesContext context) throws IOException {
if (isRendered() == false) {
return;
}
// reset the highlighted row flag
this.highlightedRow = false;
ResponseWriter out = context.getResponseWriter();
// get the child associations currently on the node and any that have been added
NodeService nodeService = Repository.getServiceRegistry(context).getNodeService();
// show the editable association component
AssociationDefinition assocDef = getAssociationDefinition(context);
if (assocDef == null) {
logger.warn("Failed to find association definition for association '" + associationName + "'");
// add an error message as the property is not defined in the data dictionary
String msg = MessageFormat.format(Application.getMessage(context, MSG_ERROR_ASSOC), new Object[] { this.associationName });
Utils.addErrorMessage(msg);
} else {
String targetType = assocDef.getTargetClass().getName().toString();
boolean allowMany = assocDef.isTargetMany();
populateAssocationMaps((Node) getValue(), nodeService);
if (isDisabled()) {
// show the current list of associations in a read-only form
renderReadOnlyAssociations(context, out, nodeService);
} else {
// start outer table
out.write("<table border='0' cellspacing='4' cellpadding='0' class='multiValueSelector'>");
if (allowMany) {
out.write("<tr><td colspan='2'>1. ");
out.write(getSelectItemsMsg());
out.write("</td></tr>");
// show the search field
renderSearchField(context, out);
// show available options for this association
renderAvailableOptions(context, out, nodeService, targetType, allowMany);
// add the Add to List button
out.write("<tr><td colspan='2'>2. <input type='submit' value='");
out.write(Application.getMessage(context, MSG_ADD_TO_LIST_BUTTON));
out.write("' onclick=\"");
out.write(generateFormSubmit(context, Integer.toString(ACTION_ADD)));
out.write("\"/>");
// add some padding
out.write("<tr><td height='6'></td></tr>");
out.write("<tr><td colspan='2'>");
out.write(getSelectedItemsMsg());
out.write("</td></tr>");
// show all the current associations
out.write("<tr><td colspan='2'><table cellspacing='0' cellpadding='2' border='0' class='selectedItems'>");
out.write("<tr><td colspan='2' class='selectedItemsHeader'>");
out.write(Application.getMessage(context, "name"));
out.write("</td></tr>");
renderExistingAssociations(context, out, nodeService, allowMany);
out.write("</table></td></tr>");
} else {
if (this.showAvailable) {
out.write("<tr><td colspan='2'>1. ");
out.write(getSelectItemMsg());
out.write("</td></tr>");
// show the search field
renderSearchField(context, out);
// show available options for this association
renderAvailableOptions(context, out, nodeService, targetType, allowMany);
// add the ok and cancel buttons
out.write("<tr><td colspan='2' align='right'><input type='submit' value='");
out.write(Application.getMessage(context, MSG_OK));
out.write("' onclick=\"");
out.write(generateFormSubmit(context, Integer.toString(ACTION_SET)));
out.write("\"/> <input type='submit' value='");
out.write(Application.getMessage(context, MSG_CANCEL));
out.write("' onclick=\"");
out.write(generateFormSubmit(context, Integer.toString(ACTION_CANCEL)));
out.write("\"/></td></tr>");
} else {
// show the select button if required
if ((allowMany == false && this.originalAssocs.size() == 0 && this.added.size() == 0) || (allowMany == false && this.originalAssocs.size() == 1 && this.removed.size() == 1 && this.added.size() == 0)) {
out.write("<tr><td><input type='submit' value='");
out.write(Application.getMessage(context, MSG_SELECT_BUTTON));
out.write("' onclick=\"");
out.write(generateFormSubmit(context, Integer.toString(ACTION_SELECT)));
out.write("\"/></td></tr>");
} else {
// show the current association
renderExistingAssociations(context, out, nodeService, allowMany);
}
}
}
if (logger.isDebugEnabled()) {
logger.debug("number original = " + this.originalAssocs.size());
logger.debug("number added = " + this.added.size());
logger.debug("number removed = " + this.removed.size());
}
// close table
out.write("</table>");
// output a hidden field containing the current value
out.write("<input type='hidden' id='");
out.write(this.getClientId(context));
out.write("_current_value");
out.write("' name='");
out.write(this.getClientId(context));
out.write("_current_value");
out.write("' value='");
// if the current state will leave the node without any associations
// do not set a value for the hidden field
int numberAssocs = (this.originalAssocs.size() + this.added.size()) - this.removed.size();
if (numberAssocs > 0) {
out.write(Integer.toString(numberAssocs));
}
out.write("' />");
}
}
}
use of javax.faces.context.ResponseWriter in project acs-community-packaging by Alfresco.
the class UIPropertySheet method renderValidationScript.
/**
* Renders the necessary JavaScript to enforce any constraints the properties
* have.
*
* @param context FacesContext
*/
@SuppressWarnings("unchecked")
protected void renderValidationScript(FacesContext context) throws IOException {
ResponseWriter out = context.getResponseWriter();
UIForm form = Utils.getParentForm(context, this);
// TODO: We need to encode all the JavaScript functions here
// with the client id of the property sheet so that we
// can potentially add more than one property sheet to
// page and have validation function correctly.
// output the validation.js script
out.write("\n<script type='text/javascript' src='");
out.write(context.getExternalContext().getRequestContextPath());
out.write("/scripts/validation.js");
out.write("'></script>\n<script type='text/javascript'>\n");
// output variable to hold flag for which submit button was pressed
out.write("var finishButtonPressed = false;\n");
out.write("var nextButtonPressed = false;\n");
out.write("var transitionButtonPressed = false;\n");
// output the validate() function
out.write("function validate()\n{\n var result = true;\n ");
out.write("if ((transitionButtonPressed || finishButtonPressed || nextButtonPressed) && (");
int numberValidations = this.validations.size();
List<ClientValidation> realTimeValidations = new ArrayList<ClientValidation>(numberValidations);
for (int x = 0; x < numberValidations; x++) {
ClientValidation validation = this.validations.get(x);
if (validation.RealTimeChecking) {
realTimeValidations.add(validation);
}
renderValidationMethod(out, validation, (x == (numberValidations - 1)), true);
}
// return false if validation failed to stop the form submitting
out.write(")\n { result = false; }\n\n");
out.write(" finishButtonPressed = false;\n nextButtonPressed = false;\n transitionButtonPressed = false;\n");
out.write(" return result;\n}\n\n");
// output the processButtonState() function (if necessary)
int numberRealTimeValidations = realTimeValidations.size();
if (numberRealTimeValidations > 0) {
out.write("function processButtonState()\n{\n if (");
for (int x = 0; x < numberRealTimeValidations; x++) {
renderValidationMethod(out, realTimeValidations.get(x), (x == (numberRealTimeValidations - 1)), false);
}
// disable the finish button if validation failed and
// also the next button if it is present
out.write("\n {\n document.getElementById('");
out.write(form.getClientId(context));
out.write(NamingContainer.SEPARATOR_CHAR);
out.write(getFinishButtonId());
out.write("').disabled = true; \n");
if (this.nextButtonId != null && this.nextButtonId.length() > 0) {
out.write(" document.getElementById('");
out.write(form.getClientId(context));
out.write(NamingContainer.SEPARATOR_CHAR);
out.write(this.nextButtonId);
out.write("').disabled = true; \n");
}
out.write(" }\n");
out.write(" else\n {\n document.getElementById('");
out.write(form.getClientId(context));
out.write(NamingContainer.SEPARATOR_CHAR);
out.write(getFinishButtonId());
out.write("').disabled = false;");
if (this.nextButtonId != null && this.nextButtonId.length() > 0) {
out.write("\n document.getElementById('");
out.write(form.getClientId(context));
out.write(NamingContainer.SEPARATOR_CHAR);
out.write(this.nextButtonId);
out.write("').disabled = false;");
}
out.write("\n }\n}\n\n");
}
// write out a function to initialise everything
out.write("function initValidation()\n{\n");
// register the validate function as the form onsubmit handler
out.write(" document.getElementById('");
out.write(form.getClientId(context));
out.write("').onsubmit = validate;\n");
// set the flag when the finish button is clicked
out.write(" document.getElementById('");
out.write(form.getClientId(context));
out.write(NamingContainer.SEPARATOR_CHAR);
out.write(getFinishButtonId());
out.write("').onclick = function() { finishButtonPressed = true; }\n");
// transition buttons on the workflow page also need to handle validation
// so look for submit buttons with ":transition_" in the id
out.write(" var inputItems = document.getElementsByTagName('input');\n");
out.write(" for (i in inputItems)\n");
out.write(" {\n");
out.write(" if (inputItems[i].type == 'submit' && inputItems[i].id !== undefined && inputItems[i].id.indexOf(':transition_') != -1)\n");
out.write(" {\n");
out.write(" inputItems[i].onclick = function() { transitionButtonPressed = true; }\n");
out.write(" }\n");
out.write(" }\n");
// set the flag when the next button is clicked
if (this.nextButtonId != null && this.nextButtonId.length() > 0) {
out.write(" document.getElementById('");
out.write(form.getClientId(context));
out.write(NamingContainer.SEPARATOR_CHAR);
out.write(this.nextButtonId);
out.write("').onclick = function() { nextButtonPressed = true; }\n");
}
// perform an initial check at page load time (if we have any real time validations)
if (numberRealTimeValidations > 0) {
out.write(" processButtonState();\n");
}
// close out the init function
out.write("}\n\n");
// setup init function to be called at page load time
out.write("window.onload=initValidation;\n");
// close out the script block
out.write("</script>\n");
}
use of javax.faces.context.ResponseWriter in project acs-community-packaging by Alfresco.
the class UILockIcon method encodeBegin.
protected void encodeBegin(final FacesContext context, final boolean locked, final boolean lockedOwner, final String[] lockUser) throws IOException {
if (isRendered() == false) {
return;
}
ResponseWriter out = context.getResponseWriter();
String msg = null;
if (locked == true) {
out.write(" <img");
outputAttribute(out, getAttributes().get("style"), "style");
outputAttribute(out, getAttributes().get("styleClass"), "class");
outputAttribute(out, getAlign(), "align");
outputAttribute(out, getWidth(), "width");
outputAttribute(out, getHeight(), "height");
out.write("src=\"");
out.write(context.getExternalContext().getRequestContextPath());
String lockImage = getLockImage();
if (lockedOwner == true && getLockOwnerImage() != null) {
lockImage = getLockOwnerImage();
}
out.write(lockImage);
out.write("\" border=0");
if (lockedOwner == true) {
msg = Application.getMessage(context, MSG_LOCKED_YOU);
if (getLockedOwnerTooltip() != null) {
msg = getLockedOwnerTooltip();
}
} else {
msg = MessageFormat.format(Application.getMessage(context, MSG_LOCKED_USER), lockUser.length);
if (getLockedUserTooltip() != null) {
msg = getLockedUserTooltip();
}
StringBuilder buf = new StringBuilder(32);
msg = buf.append(msg).append(" '").append(StringUtils.arrayToDelimitedString(lockUser, ", ")).append("'").toString();
}
msg = Utils.encode(msg);
out.write(" alt=\"");
out.write(msg);
out.write("\" title=\"");
out.write(msg);
out.write("\">");
}
}
use of javax.faces.context.ResponseWriter in project acs-community-packaging by Alfresco.
the class UINavigator method encodeBegin.
@Override
@SuppressWarnings("unchecked")
public void encodeBegin(FacesContext context) throws IOException {
if (!isRendered())
return;
// TODO: pull width and height from user preferences and/or the main config,
// if present override below using the style attribute
ResponseWriter out = context.getResponseWriter();
NavigationBean navBean = (NavigationBean) FacesHelper.getManagedBean(context, NavigationBean.BEAN_NAME);
NavigatorPluginBean navPluginBean = (NavigatorPluginBean) FacesHelper.getManagedBean(context, NavigatorPluginBean.BEAN_NAME);
List<TreeNode> rootNodesForArea = null;
String area = this.getActiveArea();
String areaTitle = null;
boolean treePanel = true;
if (NavigationBean.LOCATION_COMPANY.equals(area)) {
rootNodesForArea = navPluginBean.getCompanyHomeRootNodes();
areaTitle = Application.getMessage(context, NavigationBean.MSG_COMPANYHOME);
} else if (NavigationBean.LOCATION_HOME.equals(area)) {
rootNodesForArea = navPluginBean.getMyHomeRootNodes();
areaTitle = Application.getMessage(context, NavigationBean.MSG_MYHOME);
} else if (NavigationBean.LOCATION_GUEST.equals(area)) {
rootNodesForArea = navPluginBean.getGuestHomeRootNodes();
areaTitle = Application.getMessage(context, NavigationBean.MSG_GUESTHOME);
} else {
treePanel = false;
areaTitle = Application.getMessage(context, NavigationBean.MSG_MYALFRESCO);
}
// order the root nodes by the tree label
if (rootNodesForArea != null && rootNodesForArea.size() > 1) {
QuickSort sorter = new QuickSort(rootNodesForArea, "name", true, IDataContainer.SORT_CASEINSENSITIVE);
sorter.sort();
}
// main container div
out.write("<div id=\"navigator\" class=\"navigator\">");
// generate the active panel title
String cxPath = context.getExternalContext().getRequestContextPath();
out.write("<div class=\"sidebarButtonSelected\" style=\"background-image: url(" + cxPath + "/images/parts/navigator_blue_gradient_bg.gif)\">");
out.write("<a class='sidebarButtonSelectedLink' onclick=\"");
out.write(Utils.generateFormSubmit(context, this, getClientId(context), PANEL_ACTION + area));
out.write("\" href=\"#\">");
out.write(Utils.encode(areaTitle));
out.write("</a></div>");
// generate the javascript method to capture the tree node click events
if (treePanel) {
out.write("<script type=\"text/javascript\">");
out.write("function treeNodeSelected(nodeRef) {");
out.write(Utils.generateFormSubmit(context, this, getClientId(context), "nodeRef", true, null));
out.write("}</script>");
// generate the active panel containing the tree
out.write("<div class=\"navigatorPanelBody\">");
UITree tree = (UITree) context.getApplication().createComponent(UITree.COMPONENT_TYPE);
tree.setId("tree");
tree.setRootNodes(rootNodesForArea);
tree.setRetrieveChildrenUrl(AJAX_URL_START + ".retrieveChildren?area=" + area);
tree.setNodeCollapsedUrl(AJAX_URL_START + ".nodeCollapsed?area=" + area);
tree.setNodeSelectedCallback("treeNodeSelected");
tree.setNodeCollapsedCallback("informOfCollapse");
Utils.encodeRecursive(context, tree);
out.write("</div>");
}
// generate the closed panel title areas
String sideBarStyle = "style=\"background-image: url(" + cxPath + "/images/parts/navigator_grey_gradient_bg.gif)\"";
if (NavigationBean.LOCATION_COMPANY.equals(area) == false && navBean.getCompanyHomeVisible()) {
encodeSidebarButton(context, out, sideBarStyle, NavigationBean.LOCATION_COMPANY, NavigationBean.MSG_COMPANYHOME);
}
if (NavigationBean.LOCATION_HOME.equals(area) == false) {
encodeSidebarButton(context, out, sideBarStyle, NavigationBean.LOCATION_HOME, NavigationBean.MSG_MYHOME);
}
if (NavigationBean.LOCATION_GUEST.equals(area) == false && navBean.getIsGuest() == false && navBean.getGuestHomeVisible()) {
encodeSidebarButton(context, out, sideBarStyle, NavigationBean.LOCATION_GUEST, NavigationBean.MSG_GUESTHOME);
}
if (NavigationBean.LOCATION_MYALFRESCO.equals(area) == false) {
encodeSidebarButton(context, out, sideBarStyle, NavigationBean.LOCATION_MYALFRESCO, NavigationBean.MSG_MYALFRESCO);
}
out.write("</div>");
}
Aggregations