Added: qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java URL: http://svn.apache.org/viewvc/qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java?rev=1300204&view=auto ============================================================================== --- qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java (added) +++ qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/AbstractServlet.java Tue Mar 13 15:56:45 2012 @@ -0,0 +1,179 @@ +/* + * + * 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 org.apache.commons.codec.binary.Base64; +import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.security.*; +import org.apache.qpid.server.security.auth.AuthenticationResult; +import org.apache.qpid.server.security.auth.manager.AuthenticationManager; + +import javax.security.auth.Subject; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; +import java.security.Principal; +import java.util.Collections; + +public abstract class AbstractServlet extends HttpServlet +{ + private Subject _subject; + + @Override + protected final void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException + { + setAuthorizedSubject(request); + try + { + onGet(request, resp); + } + finally + { + clearAuthorizedSubject(); + } + } + + private void clearAuthorizedSubject() + { + _subject = null; + org.apache.qpid.server.security.SecurityManager.setThreadSubject(null); + } + + + private void setAuthorizedSubject(HttpServletRequest request) + { + HttpSession session = request.getSession(true); + Subject subject = (Subject) session.getAttribute("subject"); + + if(subject == null) + { + Principal principal = request.getUserPrincipal(); + if(principal != null) + { + subject = new Subject(false, Collections.singleton(principal),Collections.emptySet(), + Collections.emptySet()); + } + else + { + String header = request.getHeader("Authorization"); + if (header != null) + { + String[] tokens = header.split("\\s"); + if(tokens.length >= 2 + && "BASIC".equalsIgnoreCase(tokens[0])) + { + String[] credentials = (new String(Base64.decodeBase64(tokens[1].getBytes()))).split(":",2); + if(credentials.length == 2) + { + + + AuthenticationManager authenticationManager = + ApplicationRegistry.getInstance().getAuthenticationManager(); + AuthenticationResult authResult = + authenticationManager.authenticate(credentials[0], credentials[1]); + subject = authResult.getSubject(); + + } + } + } + } + } + _subject = subject; + org.apache.qpid.server.security.SecurityManager.setThreadSubject(subject); + + } + + protected Subject getSubject() + { + return _subject; + } + + protected void onGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + super.doGet(req, resp); + } + + @Override + protected final void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + setAuthorizedSubject(req); + try + { + onPost(req, resp); + } + finally + { + clearAuthorizedSubject(); + } + + } + + protected void onPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + super.doPost(req, resp); + } + + @Override + protected final void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + setAuthorizedSubject(req); + try + { + onPut(req, resp); + + } + finally + { + clearAuthorizedSubject(); + } + } + + protected void onPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + super.doPut(req, resp); + } + + @Override + protected final void doDelete(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException + { + setAuthorizedSubject(req); + try + { + onDelete(req, resp); + } + finally + { + clearAuthorizedSubject(); + } + } + + protected void onDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException + { + super.doDelete(req, resp); + } + + +}
Added: qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConnectionServlet.java URL: http://svn.apache.org/viewvc/qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConnectionServlet.java?rev=1300204&view=auto ============================================================================== --- qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConnectionServlet.java (added) +++ qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ConnectionServlet.java Tue Mar 13 15:56:45 2012 @@ -0,0 +1,128 @@ +/* + * + * 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.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.Connection; +import org.apache.qpid.server.model.Statistics; +import org.apache.qpid.server.model.VirtualHost; +import org.codehaus.jackson.map.ObjectMapper; + +public class ConnectionServlet extends AbstractServlet +{ + + + private Broker _broker; + private static final Comparator DEFAULT_COMPARATOR = new KeyComparator("name"); + + + public ConnectionServlet(Broker broker) + { + _broker = broker; + } + + protected void onGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + response.setContentType("application/json"); + response.setStatus(HttpServletResponse.SC_OK); + + String[] sortKeys = request.getParameterValues("sort"); + Comparator comparator; + if(sortKeys == null || sortKeys.length == 0) + { + comparator = DEFAULT_COMPARATOR; + } + else + { + comparator = new MapComparator(sortKeys); + } + + + Collection<VirtualHost> vhosts = _broker.getVirtualHosts(); + Collection<Connection> exchanges = new ArrayList<Connection>(); + List<Map<String,Object>> outputObject = new ArrayList<Map<String,Object>>(); + + final PrintWriter writer = response.getWriter(); + + ObjectMapper mapper = new ObjectMapper(); + String vhostName = null; + String exchangeName = null; + + if(request.getPathInfo() != null && request.getPathInfo().length()>0) + { + String path = request.getPathInfo().substring(1); + String[] parts = path.split("/"); + vhostName = parts.length == 0 ? "" : parts[0]; + + } + + for(VirtualHost vhost : vhosts) + { + if(vhostName == null || vhostName.equals(vhost.getName())) + { + for(Connection connection : vhost.getConnections()) + { + outputObject.add(convertToObject(connection)); + } + if(vhostName != null) + { + break; + } + } + } + + Collections.sort(outputObject, comparator); + mapper.writeValue(writer, outputObject); + + } + + private Map<String,Object> convertToObject(final Connection connection) + { + Map<String, Object> object = new LinkedHashMap<String, Object>(); + object.put("id",connection.getId()); + object.put("name",connection.getName()); + + object.put("session-count", connection.getSessions().size()); + + Statistics stats = connection.getStatistics(); + + object.put("bytes-out-total", stats.getStatistic("bytes-out-total")); + object.put("bytes-in-total", stats.getStatistic("bytes-in-total")); + object.put("msgs-out-total", stats.getStatistic("msgs-out-total")); + object.put("msgs-in-total", stats.getStatistic("msgs-in-total")); + + return object; + } + +} Added: qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ExchangeServlet.java URL: http://svn.apache.org/viewvc/qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ExchangeServlet.java?rev=1300204&view=auto ============================================================================== --- qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ExchangeServlet.java (added) +++ qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/ExchangeServlet.java Tue Mar 13 15:56:45 2012 @@ -0,0 +1,234 @@ +/* + * + * 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 org.apache.commons.codec.binary.Base64; +import org.apache.qpid.server.registry.ApplicationRegistry; +import org.apache.qpid.server.security.auth.AuthenticationResult; +import org.apache.qpid.server.security.auth.database.Base64MD5PasswordFilePrincipalDatabase; +import org.apache.qpid.server.security.auth.manager.AuthenticationManager; +import org.codehaus.jackson.map.ObjectMapper; + +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.Exchange; +import org.apache.qpid.server.model.LifetimePolicy; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.model.VirtualHost; +import org.apache.qpid.server.security.SecurityManager; + +import javax.security.auth.Subject; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; +import java.io.PrintWriter; +import java.security.Principal; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.StringTokenizer; + +public class ExchangeServlet extends AbstractServlet +{ + + + private Broker _broker; + private static final Comparator DEFAULT_COMPARATOR = new KeyComparator("name"); + + + public ExchangeServlet(Broker broker) + { + _broker = broker; + } + + protected void onGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + response.setContentType("application/json"); + response.setStatus(HttpServletResponse.SC_OK); + + String[] sortKeys = request.getParameterValues("sort"); + Comparator comparator; + if(sortKeys == null || sortKeys.length == 0) + { + comparator = DEFAULT_COMPARATOR; + } + else + { + comparator = new MapComparator(sortKeys); + } + + + Collection<VirtualHost> vhosts = _broker.getVirtualHosts(); + Collection<Exchange> exchanges = new ArrayList<Exchange>(); + List<Map<String,Object>> outputObject = new ArrayList<Map<String,Object>>(); + + final PrintWriter writer = response.getWriter(); + + ObjectMapper mapper = new ObjectMapper(); + String vhostName = null; + String exchangeName = null; + + if(request.getPathInfo() != null && request.getPathInfo().length()>0) + { + String path = request.getPathInfo().substring(1); + String[] parts = path.split("/"); + vhostName = parts.length == 0 ? "" : parts[0]; + if(parts.length > 1) + { + exchangeName = parts[1]; + } + } + + for(VirtualHost vhost : vhosts) + { + if(vhostName == null || vhostName.equals(vhost.getName())) + { + for(Exchange exchange : vhost.getExchanges()) + { + if(exchangeName == null || exchangeName.equals(exchange.getName())) + { + outputObject.add(convertToObject(exchange)); + if(exchangeName != null) + { + break; + } + } + } + if(vhostName != null) + { + break; + } + } + } + + Collections.sort(outputObject, comparator); + mapper.writeValue(writer, outputObject); + + } + + private Map<String,Object> convertToObject(final Exchange exchange) + { + Map<String, Object> object = new LinkedHashMap<String, Object>(); + object.put("id",exchange.getId()); + object.put("name",exchange.getName()); + object.put("type", exchange.getExchangeType()); + object.put("durable", exchange.isDurable()); + object.put("auto-delete", exchange.getLifetimePolicy() == LifetimePolicy.AUTO_DELETE); + object.put("binding-count", exchange.getBindings().size()); + + + Map<String,Object> arguments = new HashMap<String, Object>(); + for(String key : exchange.getAttributeNames()) + { + if(!key.equals(Exchange.EXCHANGE_TYPE)) + { + arguments.put(key, exchange.getAttribute(key)); + } + } + object.put("arguments", arguments); + return object; + } + + @Override + protected void onPut(final HttpServletRequest request, final HttpServletResponse response) + throws ServletException, IOException + { + + response.setContentType("application/json"); + + String vhostName = null; + String exchangeName = null; + if(request.getPathInfo() != null && request.getPathInfo().length()>0) + { + String path = request.getPathInfo().substring(1); + String[] parts = path.split("/"); + vhostName = parts.length == 0 ? "" : parts[0]; + if(parts.length > 1) + { + exchangeName = parts[1]; + } + } + + if(getSubject() == null) + { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } + else if(vhostName == null) + { + response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED); + } + else if (exchangeName == null) + { + response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED); + } + else + { + VirtualHost vhost = null; + for(VirtualHost host : _broker.getVirtualHosts()) + { + if(host.getName().equals(vhostName)) + { + vhost = host; + } + } + if(vhost == null) + { + response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED); + } + else + { + response.setStatus(HttpServletResponse.SC_NO_CONTENT); + ObjectMapper mapper = new ObjectMapper(); + Map<String,Object> exchangeObject = mapper.readValue(request.getInputStream(), LinkedHashMap.class); + + final boolean isDurable = exchangeObject.get("durable") instanceof Boolean + && ((Boolean)exchangeObject.get("durable")); + final boolean isAutoDelete = exchangeObject.get("auto_delete") instanceof Boolean + && ((Boolean)exchangeObject.get("auto_delete")); + + final String type = (String) exchangeObject.get("type"); + final Map<String, Object> attributes = new HashMap<String, Object>(exchangeObject); + attributes.remove("durable"); + attributes.remove("auto_delete"); + attributes.remove("type"); + + vhost.createExchange(exchangeName, State.ACTIVE, isDurable, + isAutoDelete ? LifetimePolicy.AUTO_DELETE : LifetimePolicy.PERMANENT, + 0l, + type, + attributes); + } + + + + } + + + + } +} \ No newline at end of file Copied: qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/KeyComparator.java (from r1300169, qpid/branches/java-config-and-management/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/QueueRegistry.java) URL: http://svn.apache.org/viewvc/qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/KeyComparator.java?p2=qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/KeyComparator.java&p1=qpid/branches/java-config-and-management/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/QueueRegistry.java&r1=1300169&r2=1300204&rev=1300204&view=diff ============================================================================== --- qpid/branches/java-config-and-management/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/QueueRegistry.java (original) +++ qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/KeyComparator.java Tue Mar 13 15:56:45 2012 @@ -18,26 +18,36 @@ * under the License. * */ -package org.apache.qpid.server.queue; +package org.apache.qpid.server.management.plugin.servlet.rest; -import org.apache.qpid.framing.AMQShortString; -import org.apache.qpid.server.virtualhost.VirtualHost; +import java.util.Comparator; +import java.util.Map; -import java.util.Collection; - -public interface QueueRegistry +class KeyComparator implements Comparator<Map> { - VirtualHost getVirtualHost(); - - void registerQueue(AMQQueue queue); - - void unregisterQueue(AMQShortString name); - - AMQQueue getQueue(AMQShortString name); + private String _key; - Collection<AMQShortString> getQueueNames(); + public KeyComparator(final String key) + { + _key = key; + } + + public int compare(final Map o1, final Map o2) + { + Comparable left = (Comparable) o1.get(_key); + Comparable right = (Comparable) o2.get(_key); + + int result; + if(left == null) + { + result = right == null ? 0 : -1; + } + else + { + result = left.compareTo(right); + } - Collection<AMQQueue> getQueues(); + return result; - AMQQueue getQueue(String queue); + } } Added: qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/MapComparator.java URL: http://svn.apache.org/viewvc/qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/MapComparator.java?rev=1300204&view=auto ============================================================================== --- qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/MapComparator.java (added) +++ qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/MapComparator.java Tue Mar 13 15:56:45 2012 @@ -0,0 +1,74 @@ +/* + * + * 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.util.Collections; +import java.util.Comparator; +import java.util.Map; + +class MapComparator implements Comparator<Map> +{ + private Comparator<Map>[] _sortKeys; + + public MapComparator(final String[] sortKeys) + { + _sortKeys = parseKeys(sortKeys); + } + + private static Comparator<Map>[] parseKeys(final String[] sortKeys) + { + Comparator<Map>[] comparators = new Comparator[sortKeys.length]; + for(int i = 0; i < sortKeys.length; i++) + { + String key = sortKeys[i]; + + if(key.startsWith("+") || key.startsWith(" ")) + { + comparators[i] = new KeyComparator(key.substring(1)); + } + else if(key.startsWith("-")) + { + comparators[i] = Collections.reverseOrder(new KeyComparator(key.substring(1))); + } + else + { + comparators[i] = new KeyComparator(key); + } + } + return comparators; + } + + + public int compare(final Map o1, final Map o2) + { + int result = 0; + for(int i = 0; i < _sortKeys.length; i++) + { + result = _sortKeys[i].compare(o1, o2); + if(result != 0) + { + return result; + } + } + return 0; + } + +} Added: qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueueServlet.java URL: http://svn.apache.org/viewvc/qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueueServlet.java?rev=1300204&view=auto ============================================================================== --- qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueueServlet.java (added) +++ qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/QueueServlet.java Tue Mar 13 15:56:45 2012 @@ -0,0 +1,225 @@ +/* + * + * 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 org.codehaus.jackson.map.ObjectMapper; + +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.Exchange; +import org.apache.qpid.server.model.LifetimePolicy; +import org.apache.qpid.server.model.Queue; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.model.VirtualHost; +import org.apache.qpid.server.queue.AMQQueue; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +public class QueueServlet extends AbstractServlet +{ + + + private Broker _broker; + private static final Comparator DEFAULT_COMPARATOR = new KeyComparator("name"); + + + public QueueServlet(Broker broker) + { + _broker = broker; + } + + protected void onGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + response.setContentType("application/json"); + response.setStatus(HttpServletResponse.SC_OK); + + String[] sortKeys = request.getParameterValues("sort"); + Comparator comparator; + if(sortKeys == null || sortKeys.length == 0) + { + comparator = DEFAULT_COMPARATOR; + } + else + { + comparator = new MapComparator(sortKeys); + } + + + Collection<VirtualHost> vhosts = _broker.getVirtualHosts(); + Collection<AMQQueue> queues = new ArrayList<AMQQueue>(); + List<Map<String,Object>> outputObject = new ArrayList<Map<String,Object>>(); + + final PrintWriter writer = response.getWriter(); + + ObjectMapper mapper = new ObjectMapper(); + String vhostName = null; + String queueName = null; + + if(request.getPathInfo() != null && request.getPathInfo().length()>0) + { + String path = request.getPathInfo().substring(1); + String[] parts = path.split("/"); + vhostName = parts.length == 0 ? "" : parts[0]; + if(parts.length > 1) + { + queueName = parts[1]; + } + } + + for(VirtualHost vhost : vhosts) + { + if(vhostName == null || vhostName.equals(vhost.getName())) + { + for(Queue queue : vhost.getQueues()) + { + if(queueName == null || queueName.equals(queue.getName())) + { + outputObject.add(convertToObject(queue)); + if(queueName != null) + { + break; + } + } + } + if(vhostName != null) + { + break; + } + } + } + + Collections.sort(outputObject, comparator); + mapper.writeValue(writer, outputObject); + + } + + private Map<String,Object> convertToObject(final Queue queue) + { + Map<String, Object> object = new LinkedHashMap<String, Object>(); + object.put("id",queue.getId()); + object.put("name",queue.getName()); + object.put("durable", queue.isDurable()); + object.put("auto-delete", queue.getLifetimePolicy() == LifetimePolicy.AUTO_DELETE); + object.put("binding-count", queue.getBindings().size()); + + + Map<String,Object> arguments = new HashMap<String, Object>(); + for(String key : queue.getAttributeNames()) + { + if(!key.equals(Exchange.EXCHANGE_TYPE)) + { + arguments.put(key, queue.getAttribute(key)); + } + } + object.put("arguments", arguments); + return object; + } + + + + @Override + protected void onPut(final HttpServletRequest request, final HttpServletResponse response) + throws ServletException, IOException + { + + response.setContentType("application/json"); + + String vhostName = null; + String queueName = null; + if(request.getPathInfo() != null && request.getPathInfo().length()>0) + { + String path = request.getPathInfo().substring(1); + String[] parts = path.split("/"); + vhostName = parts.length == 0 ? "" : parts[0]; + if(parts.length > 1) + { + queueName = parts[1]; + } + } + + if(getSubject() == null) + { + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + } + else if(vhostName == null) + { + response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED); + } + else if (queueName == null) + { + response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED); + } + else + { + VirtualHost vhost = null; + for(VirtualHost host : _broker.getVirtualHosts()) + { + if(host.getName().equals(vhostName)) + { + vhost = host; + } + } + if(vhost == null) + { + response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED); + } + else + { + response.setStatus(HttpServletResponse.SC_NO_CONTENT); + ObjectMapper mapper = new ObjectMapper(); + Map<String,Object> queueObject = mapper.readValue(request.getInputStream(), LinkedHashMap.class); + + final boolean isDurable = queueObject.get("durable") instanceof Boolean + && ((Boolean)queueObject.get("durable")); + final boolean isAutoDelete = queueObject.get("auto_delete") instanceof Boolean + && ((Boolean)queueObject.get("auto_delete")); + + final Map<String, Object> attributes = new HashMap<String, Object>(queueObject); + attributes.remove("durable"); + attributes.remove("auto_delete"); + + vhost.createQueue(queueName, State.ACTIVE, isDurable, + isAutoDelete ? LifetimePolicy.AUTO_DELETE : LifetimePolicy.PERMANENT, + 0l, + attributes); + } + + + + } + + + + } + +} Added: qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/VirtualHostServlet.java URL: http://svn.apache.org/viewvc/qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/VirtualHostServlet.java?rev=1300204&view=auto ============================================================================== --- qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/VirtualHostServlet.java (added) +++ qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/org/apache/qpid/server/management/plugin/servlet/rest/VirtualHostServlet.java Tue Mar 13 15:56:45 2012 @@ -0,0 +1,148 @@ +/* + * + * 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.IOException; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.qpid.server.model.Broker; +import org.apache.qpid.server.model.LifetimePolicy; +import org.apache.qpid.server.model.State; +import org.apache.qpid.server.model.Statistics; +import org.apache.qpid.server.model.VirtualHost; +import org.codehaus.jackson.map.ObjectMapper; + +public class VirtualHostServlet extends AbstractServlet +{ + + + private Broker _broker; + + private static final Comparator DEFAULT_COMPARATOR = new KeyComparator("name"); + + public VirtualHostServlet(Broker broker) + { + _broker = broker; + } + + protected void onGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + response.setContentType("application/json"); + response.setStatus(HttpServletResponse.SC_OK); + + Collection<VirtualHost> vhosts = _broker.getVirtualHosts(); + + String[] sortKeys = request.getParameterValues("sort"); + Comparator comparator; + if(sortKeys == null || sortKeys.length == 0) + { + comparator = DEFAULT_COMPARATOR; + } + else + { + comparator = new MapComparator(sortKeys); + } + + final PrintWriter writer = response.getWriter(); + + ObjectMapper mapper = new ObjectMapper(); + List<Map> vhostList = new ArrayList<Map>(); + + if(request.getPathInfo() == null || request.getPathInfo().length()==0) + { + + for(VirtualHost vhost : vhosts) + { + Map<String, Object> data = convertObject(vhost); + + + vhostList.add(data); + } + } + else + { + String vhostName = request.getPathInfo().substring(1); + + for(VirtualHost vhost : vhosts) + { + if(vhostName.equals(vhost.getName())) + { + Map<String, Object> data = convertObject(vhost); + + vhostList.add(data); + break; + } + } + + } + + + Collections.sort(vhostList, comparator); + + mapper.writeValue(writer, vhostList); + } + + private Map<String, Object> convertObject(VirtualHost vhost) + { + Map<String, Object> data = new HashMap<String, Object>(); + data.put("id", vhost.getId()); + data.put("name", vhost.getName()); + data.put("connection-count", vhost.getConnections().size()); + data.put("exchange-count", vhost.getExchanges().size()); + data.put("queue-count", vhost.getQueues().size()); + Statistics stats = vhost.getStatistics(); + + data.put("bytes-out-total", stats.getStatistic("bytes-out-total")); + data.put("bytes-in-total", stats.getStatistic("bytes-in-total")); + data.put("msgs-out-total", stats.getStatistic("msgs-out-total")); + data.put("msgs-in-total", stats.getStatistic("msgs-in-total")); + + return data; + } + + + protected void onPut(final HttpServletRequest request, final HttpServletResponse response) + throws ServletException, IOException + { + + response.setContentType("application/json"); + response.setStatus(HttpServletResponse.SC_NO_CONTENT); + + if(request.getPathInfo() != null && request.getPathInfo().length()>0) + { + String vhostName = request.getPathInfo().substring(1); + _broker.createVirtualHost(vhostName, State.ACTIVE, true, LifetimePolicy.PERMANENT, 0L, Collections.EMPTY_MAP); + } + + + } + +} Added: qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/.broker.html.swp URL: http://svn.apache.org/viewvc/qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/.broker.html.swp?rev=1300204&view=auto ============================================================================== Files qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/.broker.html.swp (added) and qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/.broker.html.swp Tue Mar 13 15:56:45 2012 differ Added: qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/broker.html URL: http://svn.apache.org/viewvc/qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/broker.html?rev=1300204&view=auto ============================================================================== --- qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/broker.html (added) +++ qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/broker.html Tue Mar 13 15:56:45 2012 @@ -0,0 +1,43 @@ +<!DOCTYPE HTML> +<html lang="en"> + <head> + <meta charset="utf-8"> + <title>Broker</title> + <!--<link rel="stylesheet" href="/dojo/1.7.2/dojo/resources/dojo.css"> + <link rel="stylesheet" href="/dojo/1.7.2/dijit/themes/claro/claro.css"> + <link rel="stylesheet" href="/dojo/1.7.2/dojox/grid/resources/claroGrid.css">--> + <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/resources/dojo.css"> + <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dijit/themes/claro/claro.css"> + <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojox/grid/resources/claroGrid.css"> + + + <link rel="stylesheet" href="../css/broker.css" media="screen"> + + <link rel="stylesheet" href="../css/common.css" media="screen"> + + <!-- load dojo and provide config via data attribute --> + <!--<script src="/dojo/1.7.2/dojo/dojo.js.uncompressed.js" data-dojo-config="async: true"> + </script> --> + + <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/dojo.js" + data-dojo-config="async: true"> + </script> + + <script src="/js/broker.js"> + </script> + </head> + <body class="claro"> + <h1>Broker</h1> + + <br/> + <h2>Virtual Hosts</h2> + <div id="virtualHosts"></div> + <h2>Connections</h2> + <div id="connections"></div> + <h2>Exchanges</h2> + <div id="exchanges"></div> + <h2>Queues</h2> + <div id="queues"></div> + </body> +</html> + Added: qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/css/broker.css URL: http://svn.apache.org/viewvc/qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/css/broker.css?rev=1300204&view=auto ============================================================================== --- qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/css/broker.css (added) +++ qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/css/broker.css Tue Mar 13 15:56:45 2012 @@ -0,0 +1,16 @@ +#virtualHosts { + width: 560px; + height: 110px; +} +#exchanges { + width: 560px; + height: 250px; +} +#queues { + width: 560px; + height: 200px; +} +#connections { + width: 560px; + height: 200px; +} Added: qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/css/common.css URL: http://svn.apache.org/viewvc/qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/css/common.css?rev=1300204&view=auto ============================================================================== --- qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/css/common.css (added) +++ qpid/branches/java-config-and-management/qpid/java/broker-plugins/management/src/main/java/resources/css/common.css Tue Mar 13 15:56:45 2012 @@ -0,0 +1,35 @@ +* { + outline: none !important; +} + +body { + margin: 0; + padding: 2em; + font-family: Lucida Sans,Lucida Grande,Arial !important; + font-size: 13px !important; + background: white; + color: #333; +} + +button { + -webkit-transition: background-color 0.2s linear; + border-radius:4px; + -moz-border-radius: 4px 4px 4px 4px; + -moz-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15); + background-color: #E4F2FF; + background-image: url("http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dijit/themes/claro/form/images/button.png"); + background-position: center top; + background-repeat: repeat-x; + border: 1px solid #769DC0; + padding: 2px 8px 4px; + font-size:1em; +} + +button:hover { + background-color: #AFD9FF; + color: #000000; +} + +h1 { + font-size:1.5em; +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
