All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.terracotta.management.resource.services.utils.UriInfoUtils Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
/*
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
 */
package com.terracotta.management.resource.services.utils;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.UriInfo;

/**
 * @author Ludovic Orban
 */
public class UriInfoUtils {

  private final static Set PRODUCT_IDS = Collections.unmodifiableSet(new HashSet(Arrays.asList("TMS", "WAN", "USER")));

  public static Set extractProductIds(UriInfo info) {
    List ids = info.getQueryParameters().get("productIds");
    if (ids == null) {
      return null;
    }

    Set result = new HashSet();
    for (String idsString : ids) {
      List idNames = Arrays.asList(idsString.split(","));
      for (String idName : idNames) {
        if (idName.equals("*")) {
          result.addAll(PRODUCT_IDS);
          continue;
        }
        result.add(idName);
      }
    }
    return result;
  }

  public static Set extractAgentIds(UriInfo info) {
    PathSegment agentsPathSegment = null;

    List pathSegments = info.getPathSegments();
    for (PathSegment pathSegment : pathSegments) {
      if (pathSegment.getPath().equals("agents")) {
        agentsPathSegment = pathSegment;
        break;
      }
    }

    if (agentsPathSegment == null) {
      throw new IllegalArgumentException("path does not contain /agents segment");
    }

    String value = agentsPathSegment.getMatrixParameters().getFirst("ids");

    Set values;
    if (value == null) {
      values = Collections.emptySet();
    } else {
      values = new HashSet(Arrays.asList(value.split(",")));
    }

    return values;
  }

  public static Set extractSegmentMatrixParameterAsSet(UriInfo info, String pathName, String parameterName) {
    List pathSegments = info.getPathSegments();
    for (PathSegment pathSegment : pathSegments) {
      if (pathSegment.getPath().equals(pathName)) {
        List values = pathSegment.getMatrixParameters().get(parameterName);
        return toSet(values);
      }
    }
    return null;
  }

  public static Set extractLastSegmentMatrixParameterAsSet(UriInfo info, String parameterName) {
    List values = info.getPathSegments().get(info.getPathSegments().size() - 1).getMatrixParameters().get(parameterName);
    return toSet(values);
  }

  private static Set toSet(List values) {
    if (values == null) {
      return null;
    }
    Set result = new HashSet();
    for (String value : values) {
      result.addAll(Arrays.asList(value.split(",")));
    }
    return result;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy