Package org.apache.hadoop.util

Source Code of org.apache.hadoop.util.WebUtils$JspParameterFilters

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.hadoop.corona.PoolInfo;
import org.apache.hadoop.corona.ResourceType;

/**
* Helper static utils for the jsp pages
*/
public class WebUtils {
  /**
   * True if should be shown for this filtering.
   * @param user User to check
   * @param userFilterSet Set of valid users
   * @param poolInfo Pool info to check
   * @param poolGroupFilterSet Set of valid pool groups
   * @param poolInfoFilterSet Set of valid pool infos
   * @return True if should be shown.
   */
  public static boolean showUserPoolInfo(
      String user,
      Set<String> userFilterSet, PoolInfo poolInfo,
      Set<String> poolGroupFilterSet, Set<PoolInfo> poolInfoFilterSet) {
    boolean showUser = false;
    if (userFilterSet.isEmpty() || userFilterSet.contains(user)) {
      showUser = true;
    }

    return showUser &&
        showPoolInfo(poolInfo, poolGroupFilterSet, poolInfoFilterSet);
  }

  /**
   * True if should be shown for this filtering.
   * @param poolInfo Pool info to check
   * @param poolGroupFilterSet Set of valid pool groups
   * @param poolInfoFilterSet Set of valid pool infos
   * @return True if should be shown.
   */
  public static boolean showPoolInfo(
      PoolInfo poolInfo,
      Set<String> poolGroupFilterSet,
      Set<PoolInfo> poolInfoFilterSet) {
    // If there are no filters, show everything
    if (poolGroupFilterSet.isEmpty() && poolInfoFilterSet.isEmpty()) {
      return true;
    }

    if (poolGroupFilterSet.contains(poolInfo.getPoolGroupName())) {
      return true;
    } else if (poolInfoFilterSet.contains(poolInfo)) {
      return true;
    }

    return false;
  }

  /**
   * Convert resource types of a collection of String objects
   * @param resourceTypes Collection of types to convert
   * @return Collection of resource types
   */
  public static Collection<String> convertResourceTypesToStrings(
      Collection<ResourceType> resourceTypes) {
    List<String> retList = new ArrayList<String>(resourceTypes.size());
    for (ResourceType resourceType : resourceTypes) {
      retList.add(resourceType.toString());
    }

    return retList;
  }

  /**
   * Helper class to store/retrieve the parameter filters
   */
  public static class JspParameterFilters {
    /** Users allowed, null for all */
    private final Set<String> userFilterSet = new HashSet<String>();
    /** Pool groups allowed, null for all */
    private final Set<String> poolGroupFilterSet = new HashSet<String>();
    /** Pool infos  allowed, null for all */
    private final Set<PoolInfo> poolInfoFilterSet = new HashSet<PoolInfo>();
    /** HTML output */
    private final StringBuilder htmlOutput = new StringBuilder();

    public Set<String> getUserFilterSet() {
      return userFilterSet;
    }

    public Set<String> getPoolGroupFilterSet() {
      return poolGroupFilterSet;
    }

    public Set<PoolInfo> getPoolInfoFilterSet() {
      return poolInfoFilterSet;
    }

    public StringBuilder getHtmlOutput() {
      return htmlOutput;
    }
  }

  /**
   * Check the attribute names
   * @param attributeNames Atttribute names to check
   * @return Null if all attribute names are invalid, non-empty if the check
   *         failed, with an appropriate error message
   */
  public static String validateAttributeNames(
      Enumeration<String> attributeNames) {
    while (attributeNames.hasMoreElements()) {
      String attribute = attributeNames.nextElement();
      if (!attribute.equals("users") && !attribute.equals("poolGroups") &&
          !attribute.equals("poolInfos")) {
        return "Illegal parameter " + attribute + ", only 'users, " +
            "poolGroups, and 'poolInfos' parameters allowed.";
      }
    }
    return null;
  }

  /**
   * Convert the parameters to filters and html output
   * @param userFilter User filter parameter
   * @param poolGroupFilter Pool group filter parameter
   * @param poolInfoFilter Pool info filter parameter
   * @return Filters
   */
  public static JspParameterFilters getJspParameterFilters(
      String userFilter,
      String poolGroupFilter,
      String poolInfoFilter) {
    JspParameterFilters filters = new JspParameterFilters();
    if (userFilter != null && !userFilter.equals("null")) {
      filters.getUserFilterSet().addAll(Arrays.asList(userFilter.split(",")));
      filters.getHtmlOutput().append(("<b>users:</b> " + userFilter + "<br>"));
    }
    if (poolGroupFilter != null && !poolGroupFilter.equals("null")) {
      filters.getPoolGroupFilterSet().addAll(
          Arrays.asList(poolGroupFilter.split(",")));
      filters.getHtmlOutput().append("<b>poolGroups:</b> " + poolGroupFilter +
          "<br>");
    }
    if (poolInfoFilter != null && !poolInfoFilter.equals("null")) {
      filters.getHtmlOutput().append(
          "<b>poolInfos:</b> " + poolInfoFilter + "<br>");
      for (String poolInfoString : poolInfoFilter.split(",")) {
        String[] poolInfoStrings = poolInfoString.split("[.]");
        if (poolInfoStrings.length == 2) {
          filters.getPoolInfoFilterSet().add(new PoolInfo(poolInfoStrings[0],
              poolInfoStrings[1]));
        }
      }
    }
    return filters;
  }

  /**
   * Pool info cell html
   */
  public static class PoolInfoHtml {
    private final String groupHtml;
    private final String poolHtml;

    public PoolInfoHtml(String groupHtml, String poolHtml) {
      this.groupHtml = groupHtml;
      this.poolHtml = poolHtml;
    }

    public String getGroupHtml() {
      return groupHtml;
    }

    public String getPoolHtml() {
      return poolHtml;
    }
  }

  /**
   * Generate the appropriate HTML for pool name (redirected info if necessary)
   * @param redirects
   * @param poolInfo
   * @return Pair of group and pool html
   */
  public static PoolInfoHtml getPoolInfoHtml(
      Map<PoolInfo, PoolInfo> redirects,
      PoolInfo poolInfo) {
    String redirectAttributes = null;
    if (redirects != null) {
      PoolInfo destination = redirects.get(poolInfo);
      if (destination != null) {
        redirectAttributes = "Redirected to " +
            PoolInfo.createStringFromPoolInfo(destination);
      }
    }

    String spanTag = (redirectAttributes != null) ?
        "<span class=\"ui-state-disabled\" title=\"" + redirectAttributes +
        "\">" : "<span>";
    String groupHtml = spanTag + poolInfo.getPoolGroupName() + "</span>";
    String poolHtml = spanTag +
        (poolInfo.getPoolName() == null ? "-" : poolInfo.getPoolName())
        + "</span>";

    return new PoolInfoHtml(groupHtml, poolHtml);
  }
}
TOP

Related Classes of org.apache.hadoop.util.WebUtils$JspParameterFilters

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.