Search in sources :

Example 26 with ExternalContext

use of javax.faces.context.ExternalContext in project oxTrust by GluuFederation.

the class ScopeDescriptionDownloadAction method downloadIcon.

public void downloadIcon() {
    byte[] resultFile = null;
    UmaScopeDescription scopeDescription = getScopeDescription();
    if (scopeDescription != null) {
        GluuImage gluuImage = imageService.getGluuImageFromXML(scopeDescription.getFaviconImageAsXml());
        try {
            resultFile = imageService.getThumImageData(gluuImage);
        } catch (Exception ex) {
            log.error("Failed to generate image response", ex);
        }
    }
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    if (resultFile == null) {
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
        FileDownloader.sendError(response, "Failed to prepare icon");
    } else {
        ContentDisposition contentDisposition = download ? ContentDisposition.ATTACHEMENT : ContentDisposition.NONE;
        ResponseHelper.downloadFile(scopeDescription.getId() + ".jpg", "image/jpeg", resultFile, contentDisposition, facesContext);
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ContentDisposition(org.xdi.util.io.FileDownloader.ContentDisposition) UmaScopeDescription(org.xdi.oxauth.model.uma.persistence.UmaScopeDescription) ExternalContext(javax.faces.context.ExternalContext) GluuImage(org.xdi.model.GluuImage) HttpServletResponse(javax.servlet.http.HttpServletResponse) BaseMappingException(org.gluu.persist.exception.mapping.BaseMappingException)

Example 27 with ExternalContext

use of javax.faces.context.ExternalContext in project acs-community-packaging by Alfresco.

the class ContentUpdateBean method updateFile.

/**
 * Ajax method to update file content. A multi-part form is required as the input.
 *
 * "return-page" = javascript to execute on return from the upload request
 * "nodeRef" = the nodeRef of the item to update the content of
 *
 * @throws Exception
 */
@InvokeCommand.ResponseMimetype(value = MimetypeMap.MIMETYPE_HTML)
public void updateFile() throws Exception {
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext externalContext = fc.getExternalContext();
    HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
    ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
    upload.setHeaderEncoding("UTF-8");
    List<FileItem> fileItems = upload.parseRequest(request);
    String strNodeRef = null;
    String strFilename = null;
    String strReturnPage = null;
    File file = null;
    for (FileItem item : fileItems) {
        if (item.isFormField() && item.getFieldName().equals("return-page")) {
            strReturnPage = item.getString();
        } else if (item.isFormField() && item.getFieldName().equals("nodeRef")) {
            strNodeRef = item.getString();
        } else {
            strFilename = FilenameUtils.getName(item.getName());
            file = TempFileProvider.createTempFile("alfresco", ".upload");
            item.write(file);
        }
    }
    if (logger.isDebugEnabled())
        logger.debug("Ajax content update request: " + strFilename + " to nodeRef: " + strNodeRef + " return page: " + strReturnPage);
    try {
        if (file != null && strNodeRef != null && strNodeRef.length() != 0) {
            NodeRef nodeRef = new NodeRef(strNodeRef);
            if (nodeRef != null) {
                ServiceRegistry services = Repository.getServiceRegistry(fc);
                // get a writer for the content and put the file
                ContentWriter writer = services.getContentService().getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
                writer.putContent(file);
            }
        }
    } catch (Exception e) {
        strReturnPage = strReturnPage.replace("${UPLOAD_ERROR}", e.getMessage());
    }
    Document result = XMLUtil.newDocument();
    Element htmlEl = result.createElement("html");
    result.appendChild(htmlEl);
    Element bodyEl = result.createElement("body");
    htmlEl.appendChild(bodyEl);
    Element scriptEl = result.createElement("script");
    bodyEl.appendChild(scriptEl);
    scriptEl.setAttribute("type", "text/javascript");
    Node scriptText = result.createTextNode(strReturnPage);
    scriptEl.appendChild(scriptText);
    if (logger.isDebugEnabled())
        logger.debug("Content update request complete.");
    ResponseWriter out = fc.getResponseWriter();
    XMLUtil.print(result, out);
}
Also used : FacesContext(javax.faces.context.FacesContext) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) HttpServletRequest(javax.servlet.http.HttpServletRequest) FileItem(org.apache.commons.fileupload.FileItem) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ResponseWriter(javax.faces.context.ResponseWriter) ExternalContext(javax.faces.context.ExternalContext) ServiceRegistry(org.alfresco.service.ServiceRegistry) File(java.io.File)

Example 28 with ExternalContext

use of javax.faces.context.ExternalContext in project wildfly by wildfly.

the class CountBeanViewScoped method invalidateGC.

public String invalidateGC() {
    LOG.debug("Running View Scoped Garbage Collect, invalidate session so view will be destroyed");
    System.gc();
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.invalidateSession();
    return "";
}
Also used : FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext)

Example 29 with ExternalContext

use of javax.faces.context.ExternalContext in project deltaspike by apache.

the class JsfUtils method getViewConfigPageParameters.

public static Set<RequestParameter> getViewConfigPageParameters() {
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    Set<RequestParameter> result = new HashSet<RequestParameter>();
    if (// detection of early config for different mojarra versions
    externalContext == null || externalContext.getRequestParameterValuesMap() == null || externalContext.getRequest() == null) {
        return result;
    }
    NavigationParameterContext navigationParameterContext = BeanProvider.getContextualReference(NavigationParameterContext.class);
    for (Map.Entry<String, String> entry : navigationParameterContext.getPageParameters().entrySet()) {
        // TODO add multi-value support
        result.add(new RequestParameter(entry.getKey(), new String[] { entry.getValue() }));
    }
    return result;
}
Also used : NavigationParameterContext(org.apache.deltaspike.core.api.config.view.navigation.NavigationParameterContext) ExternalContext(javax.faces.context.ExternalContext) Map(java.util.Map) HashSet(java.util.HashSet)

Example 30 with ExternalContext

use of javax.faces.context.ExternalContext in project deltaspike by apache.

the class ClientWindowHelper method handleInitialRedirect.

/**
 * Handles the initial redirect for the LAZY mode, if no windowId is available in the current request URL.
 *
 * @param facesContext the {@link FacesContext}
 * @param newWindowId the new windowId
 */
public static void handleInitialRedirect(FacesContext facesContext, String newWindowId) {
    // store the new windowId as context attribute to prevent infinite loops
    // #sendRedirect will append the windowId (from ClientWindow#getWindowId again) to the redirectUrl
    facesContext.getAttributes().put(INITIAL_REDIRECT_WINDOW_ID, newWindowId);
    ExternalContext externalContext = facesContext.getExternalContext();
    String url = constructRequestUrl(externalContext);
    // remember the initial redirect windowId till the next request - see #729
    addRequestWindowIdCookie(facesContext, newWindowId, newWindowId);
    try {
        externalContext.redirect(url);
    } catch (IOException e) {
        throw new FacesException("Could not send initial redirect!", e);
    }
}
Also used : ExternalContext(javax.faces.context.ExternalContext) IOException(java.io.IOException) FacesException(javax.faces.FacesException)

Aggregations

ExternalContext (javax.faces.context.ExternalContext)58 FacesContext (javax.faces.context.FacesContext)28 Test (org.junit.Test)17 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 HashMap (java.util.HashMap)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 Map (java.util.Map)7 IOException (java.io.IOException)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 File (java.io.File)4 Method (java.lang.reflect.Method)4 Locale (java.util.Locale)4 FacesException (javax.faces.FacesException)4 Flash (javax.faces.context.Flash)4 InvocationContext (javax.interceptor.InvocationContext)4 ExceptionQueuedEvent (javax.faces.event.ExceptionQueuedEvent)3 ExceptionQueuedEventContext (javax.faces.event.ExceptionQueuedEventContext)3 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Date (java.util.Date)2