package pt.ist.struts.dispatch.action; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.ActionMessages; import org.apache.struts.actions.DispatchAction; import pt.ist.fenixframework.DomainObject; import pt.ist.fenixframework.pstm.AbstractDomainObject; public abstract class BennuDispatchAction extends DispatchAction { protected static T getAttribute(final HttpServletRequest request, final String attributeName) { final T t = (T) request.getAttribute(attributeName); return t == null ? (T) request.getParameter(attributeName) : t; } /** * use directly */ @Deprecated protected static T getDomainObject(final String value) { return (T) AbstractDomainObject.fromExternalId(value); } protected static T getDomainObject(final HttpServletRequest request, final String attributeName) { String oid = request.getParameter(attributeName); if (oid == null || oid.length() == 0) { // This workaround must remain until there is at least one oid being // passed as a long and not its external String format. Object attribute = request.getAttribute(attributeName); if (attribute instanceof Long) { oid = attribute.toString(); } else { oid = (String) attribute; } } return (T) AbstractDomainObject.fromExternalId(oid); } // protected T getRenderedObject() { // final IViewState viewState = RenderUtils.getViewState(); // return (T) getRenderedObject(viewState); // } // // protected T getRenderedObject(final String id) { // final IViewState viewState = RenderUtils.getViewState(id); // return (T) getRenderedObject(viewState); // } // // protected T getRenderedObject(final IViewState // viewState) { // if (viewState != null) { // MetaObject metaObject = viewState.getMetaObject(); // if (metaObject != null) { // return (T) metaObject.getObject(); // } // } // return null; // } protected static ActionForward download(final HttpServletResponse response, final String filename, final byte[] bytes, final String contentType) throws IOException { try (OutputStream outputStream = response.getOutputStream()) { if (contentType != null) { response.setContentType(contentType); } if (filename != null) { response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); } response.setContentLength(bytes.length); outputStream.write(bytes); return null; } } protected static ActionForward download(final HttpServletResponse response, final String filename, InputStream stream, final String contentType) throws IOException { try (OutputStream outputStream = response.getOutputStream()) { if (contentType != null) { response.setContentType(contentType); } if (filename != null) { response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); } int byteCount = IOUtils.copy(stream, outputStream); response.setContentLength(byteCount); return null; } } protected byte[] consumeInputStream(final InputStream inputStream) throws IOException { return IOUtils.toByteArray(inputStream); } protected void addLocalizedMessage(final HttpServletRequest request, final String localizedMessage) { final ActionMessages messages = getMessages(request); ActionMessage actionMessage = new ActionMessage(localizedMessage, false); messages.add("message", actionMessage); saveMessages(request, messages); } protected void addLocalizedSuccessMessage(final HttpServletRequest request, final String localizedMessage) { final ActionMessages messages = getMessages(request); ActionMessage actionMessage = new ActionMessage(localizedMessage, false); messages.add("messageSuccess", actionMessage); saveMessages(request, messages); } protected void addLocalizedWarningMessage(final HttpServletRequest request, final String localizedMessage) { final ActionMessages messages = getMessages(request); ActionMessage actionMessage = new ActionMessage(localizedMessage, false); messages.add("messageWarning", actionMessage); saveMessages(request, messages); } protected void addMessage(final HttpServletRequest request, final String key, final String... args) { addMessage(request, "message", key, args); } protected void addMessage(final HttpServletRequest request, final String property, final String key, final String... args) { final ActionMessages messages = getMessages(request); messages.add(property, new ActionMessage(key, args)); saveMessages(request, messages); } // protected ActionForward forwardToMuneConfiguration(final // HttpServletRequest request, final VirtualHost virtualHost, // final Node node) { // request.setAttribute("virtualHostToManageId", // virtualHost.getExternalId()); // if (node != null) { // request.setAttribute("parentOfNodesToManageId", node.getExternalId()); // } // return new ActionForward("/configuration.do?method=manageMenus"); // } // // // protected ActionForward redirect(final HttpServletRequest request, final // String url) { // final String digest = // GenericChecksumRewriter.calculateChecksum(request.getContextPath() + // url); // final char seperator = url.indexOf('?') >= 0 ? '&' : '?'; // final String urlWithChecksum = url + seperator + // GenericChecksumRewriter.CHECKSUM_ATTRIBUTE_NAME + '=' + digest; // return new ActionForward(urlWithChecksum, true); // } // // protected void displayConsistencyException(ConsistencyException exc, // HttpServletRequest request) { // if (exc.getLocalizedMessage() != null) { // addLocalizedMessage(request, exc.getLocalizedMessage()); // } else { // exc.printStackTrace(); // addLocalizedMessage(request, // BundleUtil.getStringFromResourceBundle("resources/MyorgResources", // "error.ConsistencyException")); // } // } }