@Override public Object resolveReference(String key) { if (REFERENCE_REQUEST.equals(key)) { return getExternalContext().getRequest(); return getExternalContext().getSession(true); return getExternalContext().getContext(); return getExternalContext().getRequestMap(); return getExternalContext().getSessionMap(); return getExternalContext().getApplicationMap(); return getExternalContext().getRequestCookieMap(); return getExternalContext().getRequestHeaderMap(); return getExternalContext().getRequestHeaderValuesMap(); return getExternalContext().getRequestParameterMap(); return getExternalContext().getRequestParameterValuesMap(); return getExternalContext().getInitParameterMap(); return getExternalContext().getFlash();
public void download() throws IOException { // Prepare. byte[] pdfData = getItSomehow(); FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); // Initialize response. response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide. response.setContentType("application/pdf"); // Check http://www.iana.org/assignments/media-types for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename. response.setHeader("Content-disposition", "attachment; filename=\"name.pdf\""); // The Save As popup magic is done here. You can give it any filename you want, this only won't work in MSIE, it will use current request URL as filename instead. // Write file to response. OutputStream output = response.getOutputStream(); output.write(pdfData); output.close(); // Inform JSF to not take the response in hands. facesContext.responseComplete(); // Important! Else JSF will attempt to render the response which obviously will fail since it's already written with a file and closed. }
@Override public String getContextPath() { return getFacesContext().getExternalContext().getRequestContextPath(); }
@Override public Object getSessionMutex() { // Enforce presence of a session first to allow listeners to create the mutex attribute ExternalContext externalContext = getExternalContext(); Object session = externalContext.getSession(true); Object mutex = externalContext.getSessionMap().get(WebUtils.SESSION_MUTEX_ATTRIBUTE); if (mutex == null) { mutex = (session != null ? session : externalContext); } return mutex; }
/** * Return the best available mutex for the given session: * that is, an object to synchronize on for the given session. * <p>Returns the session mutex attribute if available; usually, * this means that the HttpSessionMutexListener needs to be defined * in {@code web.xml}. Falls back to the Session reference itself * if no mutex attribute found. * <p>The session mutex is guaranteed to be the same object during * the entire lifetime of the session, available under the key defined * by the {@code SESSION_MUTEX_ATTRIBUTE} constant. It serves as a * safe reference to synchronize on for locking on the current session. * <p>In many cases, the Session reference itself is a safe mutex * as well, since it will always be the same object reference for the * same active logical session. However, this is not guaranteed across * different servlet containers; the only 100% safe way is a session mutex. * @param fc the FacesContext to find the session mutex for * @return the mutex object (never {@code null}) * @see org.springframework.web.util.WebUtils#SESSION_MUTEX_ATTRIBUTE * @see org.springframework.web.util.HttpSessionMutexListener */ @Nullable public static Object getSessionMutex(FacesContext fc) { Assert.notNull(fc, "FacesContext must not be null"); ExternalContext ec = fc.getExternalContext(); Object mutex = ec.getSessionMap().get(WebUtils.SESSION_MUTEX_ATTRIBUTE); if (mutex == null) { mutex = ec.getSession(true); } return mutex; }
@Override public String getDescription(boolean includeClientInfo) { ExternalContext externalContext = getExternalContext(); StringBuilder sb = new StringBuilder(); sb.append("context=").append(externalContext.getRequestContextPath()); if (includeClientInfo) { Object session = externalContext.getSession(false); if (session != null) { sb.append(";session=").append(getSessionId()); } String user = externalContext.getRemoteUser(); if (StringUtils.hasLength(user)) { sb.append(";user=").append(user); } } return sb.toString(); }
protected void encodeContent(FacesContext context, ContentFlow cf) throws IOException { ResponseWriter writer = context.getResponseWriter(); String var = cf.getVar(); writer.startElement("div", null); writer.writeAttribute("class", "flow", null); if (var == null) { for (UIComponent child : cf.getChildren()) { if (child.isRendered()) { writer.startElement("div", null); writer.writeAttribute("class", "item", null); child.encodeAll(context); writer.endElement("div"); } } } else { Map<String, Object> requestMap = context.getExternalContext().getRequestMap(); Collection<?> value = (Collection<?>) cf.getValue(); if (value != null) { for (Iterator<?> it = value.iterator(); it.hasNext(); ) { requestMap.put(var, it.next()); writer.startElement("div", null); writer.writeAttribute("class", "item", null); renderChildren(context, cf); writer.endElement("div"); } } } writer.endElement("div"); }
private void redirect(FacesContext facesContext, WindowHandler windowHandler) { if(facesContext.getResponseComplete()) { return; } facesContext.getExternalContext().getRequestMap().put(INITIAL_REDIRCT_PERFORMED_KEY, Boolean.TRUE); try { String targetURL = facesContext.getApplication() .getViewHandler().getActionURL(facesContext, facesContext.getViewRoot().getViewId()); // add requst-parameters e.g. for f:viewParam handling windowHandler.sendRedirect(FacesContext.getCurrentInstance().getExternalContext(), targetURL, true); } catch (IOException e) { throw new UnhandledException(e); } }
public static Object getValueForComponent(FacesContext context, UIComponent component) String possibleClientId = component.getClientId(context); if (!context.getExternalContext().getRequestParameterMap().containsKey(possibleClientId)) possibleClientId = (String) context.getExternalContext() .getRequestParameterMap().get("affectedAjaxComponent"); UIViewRoot root = context.getViewRoot(); UIComponent ajaxComponent = root.findComponent(possibleClientId); if (ajaxComponent instanceof UIComponentPerspective) Object encodedValue = context.getExternalContext().getRequestParameterMap().get(possibleClientId);
public static String getResourceURL(String url, FacesContext context) { if (null == url) { return null; } Application application = context.getApplication(); ExternalContext externalContext = context.getExternalContext(); String value = url; if (value.length() == 0 || value.charAt(0) != '/' || !value.startsWith(externalContext.getRequestContextPath())) { value = application.getViewHandler().getResourceURL(context, value); } return externalContext.encodeResourceURL(value); }
protected void encodePanel(FacesContext context, AutoComplete ac) throws IOException { ResponseWriter writer = context.getResponseWriter(); String styleClass = ac.getPanelStyleClass(); styleClass = styleClass == null ? AutoComplete.PANEL_CLASS : AutoComplete.PANEL_CLASS + " " + styleClass; writer.startElement("span", null); writer.writeAttribute("id", ac.getClientId(context) + "_panel", null); writer.writeAttribute("class", styleClass, null); writer.writeAttribute("role", "listbox", null); if (ac.getPanelStyle() != null) { writer.writeAttribute("style", ac.getPanelStyle(), null); } if (ac.isDynamic() && ac.isDynamicLoadRequest(context)) { Map<String, String> params = context.getExternalContext().getRequestParameterMap(); String query = params.get(ac.getClientId(context) + "_query"); encodeResults(context, ac, query); } writer.endElement("span"); }
@Override public void encodeEnd(FacesContext context, UIComponent component) throws IOException { ResponseWriter writer = context.getResponseWriter(); Barcode barcode = (Barcode) component; String clientId = barcode.getClientId(context); Resource resource = context.getApplication().getResourceHandler().createResource("dynamiccontent.properties", "primefaces", "image/png"); String resourcePath = resource.getRequestPath(); Map<String, Object> session = context.getExternalContext().getSessionMap(); Map<String, String> barcodeMapping = (Map) session.get(Constants.BARCODE_MAPPING); if (barcodeMapping == null) { writer.startElement("img", barcode); if (shouldWriteId(component)) { writer.writeAttribute("id", clientId, "id"); writer.writeAttribute("class", styleClass, "styleClass"); writer.writeAttribute("src", context.getExternalContext().encodeResourceURL(src), null);
@Override protected void doDecode(FacesContext facesContext, UIComponent component) { Map<String, String> requestParamMap = facesContext.getExternalContext().getRequestParameterMap(); if (requestParamMap.get(component.getClientId(facesContext)) == null) { return; } String dragSourceId = (String) requestParamMap.get("dragSource"); if (Strings.isNullOrEmpty(dragSourceId)) { return; } DragSourceContextCallBack dragSourceContextCallBack = new DragSourceContextCallBack(); boolean invocationResult = facesContext.getViewRoot().invokeOnComponent(facesContext, dragSourceId, dragSourceContextCallBack); if (!invocationResult) { // TODO - log return; } AbstractDropTarget dropTarget = (AbstractDropTarget) component; new DropEvent(dropTarget, dropTarget.getDropValue(), dragSourceContextCallBack.getDragSource(), dragSourceContextCallBack.getDragValue()).queue(); }
Resource resource = context.getApplication().getResourceHandler().createResource( "dynamiccontent.properties", "primefaces", streamedContent.getContentType()); String resourcePath = resource.getRequestPath(); Map<String, Object> session = context.getExternalContext().getSessionMap(); Map<String, String> dynamicResourcesMapping = (Map) session.get(Constants.DYNAMIC_RESOURCES_MAPPING); if (dynamicResourcesMapping == null) { context.getELContext(), component.getValueExpression("value")); .append("&").append(Constants.DYNAMIC_CONTENT_TYPE_PARAM).append("=").append(type.toString()); for (int i = 0; i < component.getChildCount(); i++) { UIComponent child = component.getChildren().get(i); if (child instanceof UIParameter) { UIParameter param = (UIParameter) child; return context.getExternalContext().encodeResourceURL(src);
public boolean execute(final FacesContext facesContext) { final Application application = facesContext.getApplication(); final ViewHandler viewHandler = application.getViewHandler(); try { final UIViewRoot viewRoot = facesContext.getViewRoot(); if (viewRoot.getViewId() != null) { viewHandler.renderView(facesContext, viewRoot); } else { final Object respObj = facesContext.getExternalContext().getResponse(); if (respObj instanceof HttpServletResponse) { final HttpServletResponse respHttp = (HttpServletResponse) respObj; respHttp.sendError(HttpServletResponse.SC_NOT_FOUND); facesContext.responseComplete(); } } } catch (final IOException e) { throw new FacesException(e.getMessage(), e); } return false; }
public boolean isRTL() { if (rtl == null) { String param = context.getExternalContext().getInitParameter(Constants.ContextParams.DIRECTION); if (param == null) { rtl = false; } else { ELContext elContext = context.getELContext(); ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory(); ValueExpression expression = expressionFactory.createValueExpression(elContext, param, String.class); String expressionValue = (String) expression.getValue(elContext); rtl = (expressionValue == null) ? false : expressionValue.equalsIgnoreCase("rtl"); } } return rtl; } }
public void encodeExpansion(FacesContext context, DataTableRenderer renderer, DataTable table, int rowIndex) throws IOException { ResponseWriter writer = context.getResponseWriter(); String rowIndexVar = table.getRowIndexVar(); RowExpansion rowExpansion = table.getRowExpansion(); String styleClass = DataTable.EXPANDED_ROW_CONTENT_CLASS + " ui-widget-content"; if (rowExpansion.getStyleClass() != null) { styleClass = styleClass + " " + rowExpansion.getStyleClass(); } table.setRowIndex(rowIndex); if (rowExpansion.isRendered()) { if (rowIndexVar != null) { context.getExternalContext().getRequestMap().put(rowIndexVar, rowIndex); } writer.startElement("tr", null); writer.writeAttribute("class", styleClass, null); writer.startElement("td", null); writer.writeAttribute("colspan", table.getColumnsCount(), null); table.getRowExpansion().encodeAll(context); writer.endElement("td"); writer.endElement("tr"); } }
/** * Perform the navigation to the @LoginView. If not @LoginView is defined, return a 401 response. * The original view id requested by the user is stored in the session map, for use after a successful login. * * @param context * @param viewRoot */ private void redirectToLoginPage(FacesContext context, UIViewRoot viewRoot) { Map<String, Object> sessionMap = context.getExternalContext().getSessionMap(); preLoginEvent.fire(new PreLoginEvent(context, sessionMap)); LoginView loginView = viewConfigStore.getAnnotationData(viewRoot.getViewId(), LoginView.class); if (loginView == null || loginView.value() == null || loginView.value().isEmpty()) { log.debug("Returning 401 response (login required)"); context.getExternalContext().setResponseStatus(401); context.responseComplete(); return; } String loginViewId = loginView.value(); log.debugf("Redirecting to configured LoginView %s", loginViewId); NavigationHandler navHandler = context.getApplication().getNavigationHandler(); navHandler.handleNavigation(context, "", loginViewId); context.renderResponse(); }
@Override public void handleNavigation(FacesContext context, String fromAction, String outcome) { Map<Object, Object> attrs = context.getAttributes(); String dialogOutcome = (String) attrs.get(Constants.DIALOG_FRAMEWORK.OUTCOME); Map<String, String> requestParams = context.getExternalContext().getRequestParameterMap(); NavigationCase navCase = getNavigationCase(context, fromAction, dialogOutcome); String toViewId = navCase.getToViewId(context); String url = context.getApplication().getViewHandler().getBookmarkableURL(context, toViewId, params, includeViewParams); url = EscapeUtils.forJavaScript(url);
public static boolean isRequestSource(UIComponent component, FacesContext context) { return component.getClientId(context).equals(context.getExternalContext().getRequestParameterMap().get(Constants.RequestParams.PARTIAL_SOURCE_PARAM)); }