summaryrefslogtreecommitdiff
path: root/qpid/java/broker-plugins/management-http/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/HelperServlet.java
blob: a7066c73d83f2f7d31daac68be6613d2f396db31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 *  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.qpid.server.management.plugin.servlet.rest;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.qpid.server.management.plugin.servlet.rest.action.ListAccessControlProviderAttributes;
import org.apache.qpid.server.management.plugin.servlet.rest.action.ListAuthenticationProviderAttributes;
import org.apache.qpid.server.management.plugin.servlet.rest.action.ListGroupProviderAttributes;
import org.apache.qpid.server.management.plugin.servlet.rest.action.ListMessageStoreTypes;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

public class HelperServlet extends AbstractServlet
{
    private static final String PARAM_ACTION = "action";

    private Map<String, Action> _actions;
    private ObjectMapper _mapper;

    public HelperServlet()
    {
        _mapper = new ObjectMapper();
        _mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
        _actions = new HashMap<String, Action>();
        Action listProviderAttributes = new ListAuthenticationProviderAttributes();
        _actions.put(listProviderAttributes.getName(), listProviderAttributes);
        Action listMessageStoreTypes = new ListMessageStoreTypes();
        _actions.put(listMessageStoreTypes.getName(), listMessageStoreTypes);
        Action groupProviderAttributes = new ListGroupProviderAttributes();
        _actions.put(groupProviderAttributes.getName(), groupProviderAttributes);
        Action aclProviderAttributes = new ListAccessControlProviderAttributes();
        _actions.put(aclProviderAttributes.getName(), aclProviderAttributes);
    }

    @Override
    protected void doGetWithSubjectAndActor(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException
    {
        perform(request, response);
    }

    @Override
    protected void doPostWithSubjectAndActor(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException
    {
        perform(request, response);
    }

    private void perform(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String actionName = request.getParameter(PARAM_ACTION);
        Action action = _actions.get(actionName);
        if (action == null)
        {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        Map<String, Object> parameters = new HashMap<String, Object>();
        @SuppressWarnings("unchecked")
        Enumeration<String> names = request.getParameterNames();
        while (names.hasMoreElements())
        {
            String name = (String) names.nextElement();
            String[] values = request.getParameterValues(name);
            if (values.length == 1)
            {
                parameters.put(name, values[0]);
            }
            else
            {
                parameters.put(name, values);
            }
        }

        Object output = action.perform(parameters, getBroker());
        if (output == null)
        {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        response.setContentType("application/json");
        final Writer writer = new BufferedWriter(response.getWriter());
        _mapper.writeValue(writer, output);

        response.setStatus(HttpServletResponse.SC_OK);

    }
}