Example usage for org.apache.commons.collections EnumerationUtils toList

List of usage examples for org.apache.commons.collections EnumerationUtils toList

Introduction

In this page you can find the example usage for org.apache.commons.collections EnumerationUtils toList.

Prototype

public static List toList(Enumeration enumeration) 

Source Link

Document

Creates a list based on an enumeration.

Usage

From source file:uk.co.tfd.sm.proxy.ProxyServlet.java

@SuppressWarnings("unchecked")
@Override/*from   w  ww  .  j  a v  a  2  s .c o m*/
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        boolean proxyStream = false;
        if (POST_METHOD.equals(request.getMethod()) || PUT_METHOD.equals(request.getMethod())) {
            String proxyRequest = request.getHeader(SAKAI_PROXY_REQUEST_BODY);
            proxyStream = Boolean.parseBoolean(proxyRequest);
        }

        String path = request.getPathInfo();
        Map<String, Object> config = getConfig(path);
        if (config == null) {
            response.sendError(404);
            return;
        }

        if (!proxyStream) {
            String streamBody = (String) config.get(SAKAI_REQUEST_STREAM_BODY);
            if (streamBody != null) {
                proxyStream = Boolean.parseBoolean(streamBody);
            }
        }

        Builder<String, Object> headersBuilder = ImmutableMap.builder();
        Builder<String, Object> templateParamsBuilder = ImmutableMap.builder();

        for (Enumeration<?> enames = request.getHeaderNames(); enames.hasMoreElements();) {

            String name = (String) enames.nextElement();
            if (HEADER_BLACKLIST.contains(name)) {
                continue;
            }
            if (name.equals(BASIC_USER)) {
                templateParamsBuilder.put(BASIC_USER, request.getHeader(BASIC_USER));
            } else if (name.equals(BASIC_PASSWORD)) {
                templateParamsBuilder.put(BASIC_USER, request.getHeader(BASIC_USER));
            } else if (!name.startsWith(":")) {
                headersBuilder.put(name, toSimpleString(
                        (String[]) EnumerationUtils.toList(request.getHeaders(name)).toArray(new String[0])));
            }
        }

        Map<String, Object> headers = headersBuilder.build();

        // collect the parameters and store into a mutable map.
        Map<String, String[]> rpm = request.getParameterMap();
        for (Entry<String, String[]> e : rpm.entrySet()) {
            templateParamsBuilder.put(e.getKey(), toSimpleString(e.getValue()));
        }

        Map<String, Object> templateParams = templateParamsBuilder.build();

        // we might want to pre-process the headers
        if (config.containsKey(ProxyPreProcessor.CONFIG_PREPROCESSOR)) {
            String preprocessorName = (String) config.get(ProxyPreProcessor.CONFIG_PREPROCESSOR);
            ProxyPreProcessor preprocessor = preProcessors.get(preprocessorName);
            if (preprocessor != null) {
                preprocessor.preProcessRequest(request, headers, templateParams);
            } else {
                LOGGER.warn("Unable to find pre processor of name {} for node {} ", preprocessorName, path);
            }
        }
        ProxyPostProcessor postProcessor = defaultPostProcessor;
        // we might want to post-process the headers
        if (config.containsKey(ProxyPostProcessor.CONFIG_POSTPROCESSOR)) {
            String postProcessorName = (String) config.get(ProxyPostProcessor.CONFIG_POSTPROCESSOR);
            if (postProcessors.containsKey(postProcessorName))
                postProcessor = postProcessors.get(postProcessorName);
            if (postProcessor == null) {
                LOGGER.warn("Unable to find post processor of name {} for node {} ", postProcessorName, path);
                postProcessor = defaultPostProcessor;
            }
        }
        if (postProcessor instanceof CachingProxyProcessor) {
            CachingProxyProcessor cachingPostProcessor = (CachingProxyProcessor) postProcessor;
            if (cachingPostProcessor.sendCached(config, templateParams, response)) {
                return;
            }
        }

        ProxyResponse proxyResponse = proxyClientService.executeCall(config, headers, templateParams, null, -1,
                null);
        try {
            postProcessor.process(config, templateParams, response, proxyResponse);
        } finally {
            proxyResponse.close();
        }
    } catch (IOException e) {
        throw e;
    } catch (ProxyClientException e) {
        response.sendError(500, e.getMessage());
    }
}