use of org.alfresco.error.AlfrescoRuntimeException in project acs-community-packaging by Alfresco.
the class BaseActionWizard method initialiseActionHandlers.
/**
* Initialises the action handlers from the current configuration.
*/
protected void initialiseActionHandlers() {
if ((this.actionHandlers == null) || (Application.isDynamicConfig(FacesContext.getCurrentInstance()))) {
ConfigService svc = Application.getConfigService(FacesContext.getCurrentInstance());
Config wizardCfg = svc.getConfig("Action Wizards");
if (wizardCfg != null) {
ConfigElement actionHandlerCfg = wizardCfg.getConfigElement("action-handlers");
if (actionHandlerCfg != null) {
this.actionHandlers = new HashMap<String, IHandler>(20);
// instantiate each handler and store in the map
for (ConfigElement child : actionHandlerCfg.getChildren()) {
String actionName = child.getAttribute("name");
String handlerClass = child.getAttribute("class");
if (actionName != null && actionName.length() > 0 && handlerClass != null && handlerClass.length() > 0) {
try {
@SuppressWarnings("unchecked") Class klass = Class.forName(handlerClass);
IHandler handler = (IHandler) klass.newInstance();
this.actionHandlers.put(actionName, handler);
} catch (Exception e) {
throw new AlfrescoRuntimeException("Failed to setup action handler for '" + actionName + "'", e);
}
}
}
} else {
logger.warn("Could not find 'action-handlers' configuration element");
}
} else {
logger.warn("Could not find 'Action Wizards' configuration section");
}
}
}
use of org.alfresco.error.AlfrescoRuntimeException in project acs-community-packaging by Alfresco.
the class AjaxServlet method service.
/**
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// set default character encoding for the response
response.setCharacterEncoding("utf-8");
response.setContentType("text/xml;charset=UTF-8");
long startTime = 0;
String uri = request.getRequestURI();
if (logger.isDebugEnabled()) {
final String queryString = request.getQueryString();
logger.debug("Processing URL: " + uri + ((queryString != null && queryString.length() > 0) ? ("?" + queryString) : ""));
}
// dump the request headers
if (headersLogger.isDebugEnabled()) {
final Enumeration<?> headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
final String name = (String) headers.nextElement();
headersLogger.debug(name + ": " + request.getHeader(name));
}
}
try {
// Make sure the user is authenticated, if not throw an error to return the
// 500 Internal Server Error code back to the client
AuthenticationStatus status = servletAuthenticate(request, response, false);
if (status == AuthenticationStatus.Failure) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access Denied: User not authenticated");
return;
}
setNoCacheHeaders(response);
uri = uri.substring(request.getContextPath().length() + "/".length());
final String[] tokens = uri.split("/");
if (tokens.length < 3) {
throw new AlfrescoRuntimeException("Servlet URL did not contain all required args: " + uri);
}
// retrieve the command from the URL
final String commandName = tokens[1];
// retrieve the binding expression from the URL
final String expression = tokens[2];
// setup the faces context
final FacesContext facesContext = FacesHelper.getFacesContext(request, response, getServletContext());
// start a timer
if (perfLogger.isDebugEnabled())
startTime = System.currentTimeMillis();
// instantiate the relevant command
AjaxCommand command = null;
if (Command.invoke.toString().equals(commandName)) {
command = new InvokeCommand();
} else if (Command.get.toString().equals(commandName)) {
command = new GetCommand();
} else {
throw new AlfrescoRuntimeException("Unrecognised command received: " + commandName);
}
// execute the command
command.execute(facesContext, expression, request, response);
} catch (RuntimeException error) {
handleError(response, error);
} finally {
// measure the time taken
if (perfLogger.isDebugEnabled()) {
perfLogger.debug("Time to execute command: " + (System.currentTimeMillis() - startTime) + "ms");
}
ContextHolder.setContext(null);
}
}
use of org.alfresco.error.AlfrescoRuntimeException in project acs-community-packaging by Alfresco.
the class GetCommand method execute.
public void execute(FacesContext facesContext, String expression, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// create the JSF binding expression
String bindingExpr = makeBindingExpression(expression);
if (logger.isDebugEnabled())
logger.debug("Retrieving value from value binding: " + bindingExpr);
UserTransaction tx = null;
try {
// create the value binding
ValueBinding binding = facesContext.getApplication().createValueBinding(bindingExpr);
if (binding != null) {
// setup the transaction
tx = Repository.getUserTransaction(facesContext, true);
tx.begin();
// get the value from the value binding
Object value = binding.getValue(facesContext);
if (value != null) {
response.getWriter().write(value.toString());
}
// commit
tx.commit();
}
} catch (Throwable err) {
// rollback the transaction
try {
if (tx != null) {
tx.rollback();
}
} catch (Exception ex) {
}
throw new AlfrescoRuntimeException("Failed to retrieve value: " + err.getMessage(), err);
}
}
use of org.alfresco.error.AlfrescoRuntimeException 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 org.alfresco.error.AlfrescoRuntimeException in project acs-community-packaging by Alfresco.
the class EditContentPropertiesCommand method execute.
/**
* @see org.alfresco.web.app.servlet.command.Command#execute(org.alfresco.service.ServiceRegistry, java.util.Map)
*/
public Object execute(ServiceRegistry serviceRegistry, Map<String, Object> properties) {
ServletContext sc = (ServletContext) properties.get(PROP_SERVLETCONTEXT);
ServletRequest req = (ServletRequest) properties.get(PROP_REQUEST);
ServletResponse res = (ServletResponse) properties.get(PROP_RESPONSE);
FacesContext fc = FacesHelper.getFacesContext(req, res, sc, "/jsp/close.jsp");
BrowseBean browseBean = (BrowseBean) FacesHelper.getManagedBean(fc, BrowseBean.BEAN_NAME);
// setup context from url args in properties map
String strNodeRef = (String) properties.get(PROP_NODEREF);
ParameterCheck.mandatoryString(PROP_NODEREF, strNodeRef);
browseBean.setDocument(new Node(new NodeRef(strNodeRef)));
NavigationHandler navigationHandler = fc.getApplication().getNavigationHandler();
navigationHandler.handleNavigation(fc, null, "dialog:editContentProperties");
String viewId = fc.getViewRoot().getViewId();
try {
sc.getRequestDispatcher(BaseServlet.FACES_SERVLET + viewId).forward(req, res);
} catch (Exception e) {
throw new AlfrescoRuntimeException("Unable to forward to viewId: " + viewId, e);
}
return null;
}
Aggregations