org.apache.commons.collections.EnumerationUtils Java Examples

The following examples show how to use org.apache.commons.collections.EnumerationUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JarUtils.java    From confucius-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Filter {@link JarEntry} list from {@link JarFile}
 *
 * @param jarFile
 *         {@link JarFile}
 * @param jarEntryFilter
 *         {@link JarEntryFilter}
 * @return Read-only List
 */
@Nonnull
public static List<JarEntry> filter(JarFile jarFile, JarEntryFilter jarEntryFilter) {
    if (jarFile == null) {
        return Collections.emptyList();
    }
    Enumeration<JarEntry> jarEntries = jarFile.entries();
    List<JarEntry> jarEntriesList = EnumerationUtils.toList(jarEntries);
    return doFilter(jarEntriesList, jarEntryFilter);
}
 
Example #2
Source File: MultiSessionAttributeAdapter.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
private static boolean requestIsHtml(HttpServletRequest optionalRequest) {
    Set headerList = separateOneLineMediaTypes(EnumerationUtils.toList(optionalRequest.getHeaders(HttpHeaders.ACCEPT)));
    Set defaultMediaTypes = ImmutableSet.of(MediaType.TEXT_HTML, MediaType.APPLICATION_XHTML_XML, MediaType.APPLICATION_XML);
    if(CollectionUtils.containsAny(headerList,defaultMediaTypes)){
        return true;
    }
    return false;
}
 
Example #3
Source File: KSBDispatcherServlet.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * This is a workaround after upgrading to CXF 2.7.0 whereby we could no longer just call "setHideServiceList" on
 * the ServletController. Instead, it is now reading this information from the ServletConfig, so wrapping the base
 * ServletContext to return true or false for hide service list depending on whether or not we are in dev mode.
 */
protected ServletConfig getCXFServletConfig(final ServletConfig baseServletConfig) {
    // disable handling of URLs ending in /services which display CXF generated service lists if we are not in dev mode
    final String shouldHide = Boolean.toString(!ConfigContext.getCurrentContextConfig().getDevMode().booleanValue());
    return new ServletConfig() {
        private static final String HIDE_SERVICE_LIST_PAGE_PARAM = "hide-service-list-page";
        @Override
        public String getServletName() {
            return baseServletConfig.getServletName();
        }
        @Override
        public ServletContext getServletContext() {
            return baseServletConfig.getServletContext();
        }
        @Override
        public String getInitParameter(String parameter) {
            if (HIDE_SERVICE_LIST_PAGE_PARAM.equals(parameter)) {
                return shouldHide;
            }
            return baseServletConfig.getInitParameter(parameter);
        }
        @Override
        public Enumeration<String> getInitParameterNames() {
            List<String> initParameterNames = EnumerationUtils.toList(baseServletConfig.getInitParameterNames());
            initParameterNames.add(HIDE_SERVICE_LIST_PAGE_PARAM);
            return new Vector<String>(initParameterNames).elements();
        }
    };
}
 
Example #4
Source File: CsrfTokenFilter.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    // if header supplied, check it is valid
    String requiredWhenS = request.getHeader(CSRF_TOKEN_REQUIRED_HEADER);
    if (Strings.isNonBlank(requiredWhenS) && getRequiredForRequests(requiredWhenS, null)==null) {
        fail(requestContext, ApiError.builder().errorCode(Response.Status.BAD_REQUEST)
            .message(HEADER_OF_COOKIE(CSRF_TOKEN_REQUIRED_HEADER)+" header if supplied must be one of "
                + Arrays.asList(CsrfTokenRequiredForRequests.values()))
            .build());
        return;
}
    
    if (!request.isRequestedSessionIdValid()) {
        // no session; just return
        return;
    }
    
    @SuppressWarnings("unchecked")
    List<String> suppliedTokensDefault = EnumerationUtils.toList(request.getHeaders(CSRF_TOKEN_VALUE_HEADER));
    @SuppressWarnings("unchecked")
    List<String> suppliedTokensAngular = EnumerationUtils.toList(request.getHeaders(CSRF_TOKEN_VALUE_HEADER_ANGULAR_NAME));
    List<String> suppliedTokens = Lists.newArrayList(suppliedTokensDefault);
    suppliedTokens.addAll(suppliedTokensAngular);

    MultiSessionAttributeAdapter session = MultiSessionAttributeAdapter.of(request);
    Object requiredToken = session.getAttribute(CSRF_TOKEN_VALUE_ATTR);
    CsrfTokenRequiredForRequests whenRequired = (CsrfTokenRequiredForRequests) session.getAttribute(CSRF_TOKEN_REQUIRED_ATTR);

    boolean isRequired;
    
    if (whenRequired==null) {
        if (suppliedTokens.isEmpty()) {
            log.warn("No CSRF token expected or supplied but a cookie-session is active: client should be updated"
                + " (in future CSRF tokens or instructions may be required for session-based clients)"
                + " - " + Entitlements.getEntitlementContext());
            whenRequired = CsrfTokenRequiredForRequests.NONE;
        } else {
            // default
            whenRequired = DEFAULT_REQUIRED_FOR_REQUESTS;
        }
        // remember it to suppress warnings subsequently
        session.setAttribute(CSRF_TOKEN_REQUIRED_ATTR, whenRequired);
    }
    
    switch (whenRequired) {
    case NONE:
        isRequired = false;
        break;
    case WRITE:
        isRequired = !org.eclipse.jetty.http.HttpMethod.GET.toString().equals(requestContext.getMethod());
        break;
    case ALL:
        isRequired = true;
        break;
    default:
        log.warn("Unexpected "+CSRF_TOKEN_REQUIRED_ATTR+" value "+whenRequired);
        isRequired = true;
    }
    
    if (Iterables.any(suppliedTokens, Predicates.equalTo(requiredToken))) {
        // matching token supplied, or not being used 
        return;
    }
    
    if (!isRequired) {
        // csrf not required, but it doesn't match
        if (requiredToken!=null) {
            log.trace("CSRF optional token mismatch: client did not send valid token, but it isn't required so proceeding");
        }
        return;
    }

    if (suppliedTokens.isEmpty()) {
        fail(requestContext, ApiError.builder().errorCode(Response.Status.FORBIDDEN)
                .message(HEADER_OF_COOKIE(CSRF_TOKEN_VALUE_COOKIE)+" header is required, containing token previously returned from server in cookie")
                .build());
    } else {
        fail(requestContext, ApiError.builder().errorCode(Response.Status.FORBIDDEN)
            .message(HEADER_OF_COOKIE(CSRF_TOKEN_VALUE_COOKIE)+" header did not match expected CSRF token")
            .build());
    }
}
 
Example #5
Source File: HttpServletRequestSession.java    From usergrid with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked" })
@Override
public Collection<Object> getAttributeKeys() throws InvalidSessionException {
    return EnumerationUtils.toList( request.getAttributeNames() );
}
 
Example #6
Source File: ClassLoaderUtils.java    From confucius-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Get the resource URLs Set under specified resource name and type
 *
 * @param classLoader
 *         ClassLoader
 * @param resourceType
 *         {@link ResourceType} Enum
 * @param resourceName
 *         resource name ,e.g : <br /> <ul> <li>Resource Name :<code>"/com/abc/def.log"</code></li> <li>Class Name :
 *         <code>"java.lang.String"</code></li> </ul>
 * @return the resource URL under specified resource name and type
 * @throws NullPointerException
 *         If any argument is <code>null</code>
 * @throws IOException
 * @version 1.0.0
 * @since 1.0.0
 */
public static Set<URL> getResources(ClassLoader classLoader, ResourceType resourceType, String resourceName) throws NullPointerException, IOException {
    String normalizedResourceName = resourceType.resolve(resourceName);
    Enumeration<URL> resources = classLoader.getResources(normalizedResourceName);
    return resources != null && resources.hasMoreElements() ? Sets.newLinkedHashSet(EnumerationUtils.toList(resources)) : Collections.<URL>emptySet();
}