org.glassfish.admingui.common.handlers.CommonHandlers Maven / Gradle / Ivy
Show all versions of payara-micro Show documentation
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2016 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
/*
* CommonHandlers.java
*
* Created on August 30, 2006, 4:21 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.glassfish.admingui.common.handlers;
import com.sun.enterprise.config.serverbeans.ServerTags;
import com.sun.jsftemplating.annotation.Handler;
import com.sun.jsftemplating.annotation.HandlerInput;
import com.sun.jsftemplating.annotation.HandlerOutput;
import com.sun.jsftemplating.el.PageSessionResolver;
import com.sun.jsftemplating.handlers.NavigationHandlers;
import com.sun.jsftemplating.layout.descriptors.handler.HandlerContext;
import com.sun.jsftemplating.util.Util;
import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import org.glassfish.admingui.common.tree.FilterTreeEvent;
import org.glassfish.admingui.common.util.GuiUtil;
import org.glassfish.admingui.common.util.MiscUtil;
import org.glassfish.admingui.common.util.RestUtil;
import org.glassfish.admingui.common.util.TargetUtil;
public class CommonHandlers {
private static final String AIX = "AIX";
/** Creates a new instance of CommonHandlers */
public CommonHandlers() {
}
/**
* This handler will be called during initialization when Cluster Support is detected.
*/
@Handler(id="initClusterSessionAttribute")
public static void initClusterSessionAttribute(HandlerContext handlerCtx){
Map sessionMap = handlerCtx.getFacesContext().getExternalContext().getSessionMap();
//The summary or detail view of deploy tables is stored in session to remember user's previous
//preference.
sessionMap.put("appSummaryView", true);
sessionMap.put("webSummaryView", true);
sessionMap.put("ejbSummaryView", true);
sessionMap.put("appclientSummaryView", true);
sessionMap.put("rarSummaryView", true);
sessionMap.put("lifecycleSummaryView", true);
sessionMap.put("adminObjectSummaryView", true);
sessionMap.put("connectorResSummaryView", true);
sessionMap.put("customResSummaryView", true);
sessionMap.put("externalResSummaryView", true);
sessionMap.put("javaMailSessionSummaryView", true);
sessionMap.put("jdbcResSummaryView", true);
sessionMap.put("jmsConnectionSummaryView", true);
sessionMap.put("jmsDestinationSummaryView", true);
}
/**
*
This handler will be called during initialization for doing any initialization.
*/
@Handler(id="initSessionAttributes")
public static void initSessionAttributes(HandlerContext handlerCtx){
//Ensure this method is called once per session
Object initialized = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("_SESSION_INITIALIZED");
if (initialized == null){
GuiUtil.initSessionAttributes();
}
return;
}
/**
*
* This handler will be called from baseLayout.xhtml to load the maximum
* field lengths (maximum number of characters that a user can enter in each
* field).
*/
@Handler(id="getFieldLengths",
input={
@HandlerInput(name="bundle", type=java.util.ResourceBundle.class, required=true)
},
output={
@HandlerOutput(name="result", type=Map.class)
})
public static void getFieldLengths(HandlerContext handlerCtx) {
ResourceBundle bundle = (ResourceBundle) handlerCtx.getInputValue("bundle");
Map result = new HashMap();
for (String key : bundle.keySet()) {
try {
result.put(key, Integer.decode(bundle.getString(key)));
} catch (NumberFormatException ex) {
// Log warning about expecting a number...
// This should never happen; if it does it's a bug, so no need to localize.
GuiUtil.getLogger().warning(
"Field length is expected to be a number, but got ('"
+ bundle.getString(key) + "') instead for key '"
+ key + "'.");
}
}
handlerCtx.setOutputValue("result", result);
}
/** This function is called in login.jsf to set the various product specific attributes such as the
* product GIFs and product names. A similar function is called for Sailfin to set Sailfin specific
* product GIFs and name.
* The function is defined in com.sun.extensions.comms.SipUtilities
*/
@Handler(id="initProductInfoAttributes")
public static void initProductInfoAttributes(HandlerContext handlerCtx) {
Map sessionMap = handlerCtx.getFacesContext().getExternalContext().getSessionMap();
//Ensure this method is called once per session
Object initialized = sessionMap.get("_INFO_SESSION_INITIALIZED");
if (initialized != null) {
return;
}
// Initialize Product Specific Attributes
sessionMap.put("productImageURL", GuiUtil.getMessage("productImage.URL"));
sessionMap.put("productImageWidth", Integer.parseInt(GuiUtil.getMessage("productImage.width")));
sessionMap.put("productImageHeight", Integer.parseInt(GuiUtil.getMessage("productImage.height")));
sessionMap.put("loginProductImageURL", GuiUtil.getMessage("login.productImage.URL"));
sessionMap.put("loginProductImageWidth", Integer.parseInt(GuiUtil.getMessage("login.productImage.width")));
sessionMap.put("loginProductImageHeight", Integer.parseInt(GuiUtil.getMessage("login.productImage.height")));
sessionMap.put("fullProductName", GuiUtil.getMessage("versionImage.description"));
sessionMap.put("loginButtonTooltip", GuiUtil.getMessage("loginButtonTooltip"));
sessionMap.put("mastHeadDescription", GuiUtil.getMessage("mastHeadDescription"));
// showLoadBalancer is a Sailfin specific attribute. Sailfin uses Converged LB instead
// of HTTP LB. It is true for GF and false for Sailfin. In sailfin this is set in
// com.sun.extensions.comms.SipUtilities.initProductInfoAttributes() called for Sailfin in login.jsf
//TODO-V3 may need to set this back to true
//sessionMap.put("showLoadBalancer", true);
sessionMap.put("_INFO_SESSION_INITIALIZED","TRUE");
}
/**
* This handler returns String[] of the given java.util.List
*
* Output value: "selectedIndex" -- Type: Object
/
* @param handlerCtx The HandlerContext.
*/
@Handler(id="getListElement",
input={
@HandlerInput(name="list", type=java.util.List.class, required=true ),
@HandlerInput(name="index", type=Integer.class)},
output={
@HandlerOutput(name="selectedIndex", type=Object.class)})
public static void getListElement(HandlerContext handlerCtx) {
List list = (List)handlerCtx.getInputValue("list");
Integer selectedIndex = (Integer)handlerCtx.getInputValue("index");
String[] listItem = null;
if(list != null) {
if(selectedIndex == null) {
//default to 0
selectedIndex = Integer.valueOf(INDEX);
}
listItem = new String[]{list.get(selectedIndex)};
}
handlerCtx.setOutputValue("selectedIndex", listItem);
}
/**
* This handler removes the given element from the list
*
* Output value: "finalList" -- Type: List
/
* @param handlerCtx The HandlerContext.
*/
@Handler(id="removeListElement",
input={
@HandlerInput(name="list", type=java.util.List.class, required=true ),
@HandlerInput(name="name", type=String.class)},
output={
@HandlerOutput(name="finalList", type=java.util.List.class)})
public static void removeListElement(HandlerContext handlerCtx) {
List list = (List) handlerCtx.getInputValue("list");
String name = (String) handlerCtx.getInputValue("name");
String[] listItem = null;
if (list != null) {
list.remove(name);
}
handlerCtx.setOutputValue("finalList", list);
}
/**
* This handler converts the milliseconds to readable format
*
* Output value: "readableString" -- Type: String
/
* @param handlerCtx The HandlerContext.
*/
@Handler(id="convertMillisToReadable",
input={
@HandlerInput(name="milliseconds", type=Long.class, required=true )},
output={
@HandlerOutput(name="readableString", type=String.class)})
public static void convertMillisToReadable(HandlerContext handlerCtx) {
Long milliseconds = (Long) handlerCtx.getInputValue("milliseconds");
final long MSEC_PER_SECOND = 1000;
final long MSEC_PER_MINUTE = 60 * MSEC_PER_SECOND;
final long MSEC_PER_HOUR = MSEC_PER_MINUTE * 60;
final long MSEC_PER_DAY = MSEC_PER_HOUR * 24;
final long MSEC_PER_WEEK = MSEC_PER_DAY * 7;
String FORMAT2 = "%d %s %d %s";
String FORMAT1 = "%d %s";
String readableString = "";
long msecLeftover = milliseconds;
long numWeeks = msecLeftover / MSEC_PER_WEEK;
msecLeftover -= numWeeks * MSEC_PER_WEEK;
long numDays = msecLeftover / MSEC_PER_DAY;
msecLeftover -= numDays * MSEC_PER_DAY;
long numHours = msecLeftover / MSEC_PER_HOUR;
msecLeftover -= numHours * MSEC_PER_HOUR;
long numMinutes = msecLeftover / MSEC_PER_MINUTE;
msecLeftover -= numMinutes * MSEC_PER_MINUTE;
long numSeconds = msecLeftover / MSEC_PER_SECOND;
msecLeftover -= numSeconds * MSEC_PER_SECOND;
long numMilliSeconds = msecLeftover;
if (numWeeks > 0) {
readableString = String.format(FORMAT2, numWeeks, GuiUtil.getMessage("common.Weeks"), numDays, GuiUtil.getMessage("common.Days"));
} else if (numDays > 0) {
readableString = String.format(FORMAT1, numDays, GuiUtil.getMessage("common.Days"));
} else if (numHours > 0) {
readableString = String.format(FORMAT2, numHours, GuiUtil.getMessage("common.Hours"), numMinutes, GuiUtil.getMessage("common.Minutes"));
} else if (numMinutes > 0) {
readableString = String.format(FORMAT2, numMinutes, GuiUtil.getMessage("common.Minutes"), numSeconds, GuiUtil.getMessage("common.Seconds"));
} else if (numSeconds > 0) {
readableString = String.format(FORMAT1, numSeconds, GuiUtil.getMessage("common.Seconds"));
} else {
readableString = String.format(FORMAT1, numMilliSeconds, GuiUtil.getMessage("common.Milliseconds"));
}
handlerCtx.setOutputValue("readableString", readableString);
}
/**
* This handler creates a map with the given keys and values
*
* Output value: "map" -- Type: Map
/
* @param handlerCtx The HandlerContext.
*/
@Handler(id="gf.createAttributeMap",
input={
@HandlerInput(name="keys", type=java.util.List.class),
@HandlerInput(name="values", type=java.util.List.class)},
output={
@HandlerOutput(name="map", type=java.util.Map.class)})
public static void createAttributeMap(HandlerContext handlerCtx) {
List keys = (List) handlerCtx.getInputValue("keys");
List values = (List) handlerCtx.getInputValue("values");
Map map = new HashMap();
if (keys != null && values != null) {
for (int i = 0; i < keys.size(); i++) {
map.put(keys.get(i), values.get(i));
}
}
handlerCtx.setOutputValue("map", map);
}
/**
* This handler returns the encoded String using the type specified.
*
If type is not specified, it defaults to UTF-8.
*
Input value: "value" -- Type: String
*
Input value: "delim" -- Type: String
*
Input Value: "type" -- Type: String
*
Output Value: "value" -- Type: String
*@param handlerCtx The HandlerContext.
*/
@Handler(id="selectiveEncode",
input={
@HandlerInput(name="value", type=String.class, required=true ),
@HandlerInput(name="delim", type=String.class),
@HandlerInput(name="type", type=String.class)},
output={
@HandlerOutput(name="result", type=String.class)}
)
public static void selectiveEncode(HandlerContext handlerCtx) {
String value = (String) handlerCtx.getInputValue("value");
String delim = (String) handlerCtx.getInputValue("delim");
String encType = (String) handlerCtx.getInputValue("type");
String encodedString = GuiUtil.encode(value, delim, encType);
handlerCtx.setOutputValue("result", encodedString);
}
/**
*
This method kills the session, and logs out
* Server Domain Attributes Page.
* Input value: "page" -- Type: java.lang.String
* @param handlerCtx The HandlerContext.
*/
@Handler(id="logout")
public static void logout(HandlerContext handlerCtx) {
handlerCtx.getFacesContext().getExternalContext().invalidateSession();
}
/**
* This method sets the required attribute of a UI component .
*
Input value: "id" -- Type: java.lang.String
* Input value: "required" -- Type: java.lang.String
* @param handlerCtx The HandlerContext.
*/
@Handler(id="setComponentRequired",
input={
@HandlerInput(name="id", type=String.class, required=true),
@HandlerInput(name="required", type=String.class, required=true)
})
public static void setComponentRequired(HandlerContext handlerCtx) {
String id = (String) handlerCtx.getInputValue("id");
String required = (String) handlerCtx.getInputValue("required");
UIComponent viewRoot = handlerCtx.getFacesContext().getViewRoot();
if (viewRoot == null) return;
try {
UIInput targetComponent = (UIInput) viewRoot.findComponent(id);
if (targetComponent != null ){
targetComponent.setRequired(Boolean.valueOf(required));
}
}catch(Exception ex){
//Cannot find component, do nothing.
}
}
/**
* Test if a particular attribute exists.
* It will look at request scope, then page, then session.
*/
@Handler(id="testExists",
input={
@HandlerInput(name="attr", type=String.class, required=true )},
output={
@HandlerOutput(name="defined", type=Boolean.class)}
)
public static void testExists(HandlerContext handlerCtx) {
String attr = (String) handlerCtx.getInputValue("attr");
if(GuiUtil.isEmpty(attr)){
handlerCtx.setOutputValue("defined", false);
}else{
handlerCtx.setOutputValue("defined", true);
}
}
/**
*
Remove the properties if the key(name) is empty.
*
*/
@Handler(id="removeEmptyProps",
input={
@HandlerInput(name="props", type=List.class, required=true )},
output={
@HandlerOutput(name="modifiedProps", type=List.class)}
)
public static void removeEmptyProps(HandlerContext handlerCtx) {
List