Author: chirino
Date: Wed Dec 21 19:48:25 2011
New Revision: 1221856
URL: http://svn.apache.org/viewvc?rev=1221856&view=rev
Log:
Fixes APLO-122: NOT(Boolean) query function added. Return a ErrorDTO holding
the error message for all 40x and 50x status codes. Really return a 500 error
result and log as a warning on all unexpected exceptions.
Added:
activemq/activemq-apollo/trunk/apollo-dto/src/main/java/org/apache/activemq/apollo/dto/ErrorDTO.java
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/JaxrsExceptionMapper.scala
Modified:
activemq/activemq-apollo/trunk/apollo-dto/src/main/resources/org/apache/activemq/apollo/dto/jaxb.index
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/BrokerResource.scala
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/Support.scala
activemq/activemq-apollo/trunk/apollo-web/src/main/webapp/WEB-INF/web.xml
activemq/activemq-apollo/trunk/apollo-website/src/documentation/management-api.md
Added:
activemq/activemq-apollo/trunk/apollo-dto/src/main/java/org/apache/activemq/apollo/dto/ErrorDTO.java
URL:
http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-dto/src/main/java/org/apache/activemq/apollo/dto/ErrorDTO.java?rev=1221856&view=auto
==============================================================================
---
activemq/activemq-apollo/trunk/apollo-dto/src/main/java/org/apache/activemq/apollo/dto/ErrorDTO.java
(added)
+++
activemq/activemq-apollo/trunk/apollo-dto/src/main/java/org/apache/activemq/apollo/dto/ErrorDTO.java
Wed Dec 21 19:48:25 2011
@@ -0,0 +1,68 @@
+/**
+ * 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.activemq.apollo.dto;
+
+import javax.xml.bind.annotation.*;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * <p>
+ * </p>
+ *
+ * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
+ */
+@XmlRootElement(name="error")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class ErrorDTO {
+
+ /**
+ * The message associated with the error.
+ */
+ @XmlAttribute(name="message")
+ public String message;
+
+ public ErrorDTO(){}
+
+ public ErrorDTO(String message) {
+ this.message = message;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof ErrorDTO)) return false;
+
+ ErrorDTO errorDTO = (ErrorDTO) o;
+
+ if (message != null ? !message.equals(errorDTO.message) :
errorDTO.message != null) return false;
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ return message != null ? message.hashCode() : 0;
+ }
+
+ @Override
+ public String toString() {
+ return "ErrorDTO{" +
+ "message='" + message + '\'' +
+ '}';
+ }
+}
Modified:
activemq/activemq-apollo/trunk/apollo-dto/src/main/resources/org/apache/activemq/apollo/dto/jaxb.index
URL:
http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-dto/src/main/resources/org/apache/activemq/apollo/dto/jaxb.index?rev=1221856&r1=1221855&r2=1221856&view=diff
==============================================================================
---
activemq/activemq-apollo/trunk/apollo-dto/src/main/resources/org/apache/activemq/apollo/dto/jaxb.index
(original)
+++
activemq/activemq-apollo/trunk/apollo-dto/src/main/resources/org/apache/activemq/apollo/dto/jaxb.index
Wed Dec 21 19:48:25 2011
@@ -32,6 +32,7 @@ DestinationDTO
DurableSubscriptionDTO
DurableSubscriptionDestinationDTO
EntryStatusDTO
+ErrorDTO
IntMetricDTO
JvmMetricsDTO
KeyStorageDTO
Modified:
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/BrokerResource.scala
URL:
http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/BrokerResource.scala?rev=1221856&r1=1221855&r2=1221856&view=diff
==============================================================================
---
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/BrokerResource.scala
(original)
+++
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/BrokerResource.scala
Wed Dec 21 19:48:25 2011
@@ -22,7 +22,8 @@ import scala.collection.Iterable
import org.apache.activemq.apollo.util.path.PathParser
import org.apache.activemq.apollo.util._
import javax.ws.rs._
-import javax.ws.rs.core.Context
+import core.Response.Status
+import core.{Response, Context}
import javax.ws.rs.core.Response.Status._
import management.ManagementFactory
import javax.management.ObjectName
@@ -46,6 +47,7 @@ import javax.ws.rs.core.MediaType._
*/
@Produces(Array(APPLICATION_JSON, APPLICATION_XML, TEXT_XML, "text/html;qs=5"))
case class BrokerResource() extends Resource {
+ import Resource._
@GET
@Path("whoami")
@@ -429,6 +431,17 @@ case class BrokerResource() extends Reso
memo.flatMap(invoke(_, field))
}.getOrElse(null)
}
+
+ def NOT(o:AnyRef):AnyRef = not(o)
+ def Not(o:AnyRef):AnyRef = not(o)
+ def not(o:AnyRef):AnyRef = {
+ o match {
+ case java.lang.Boolean.TRUE => java.lang.Boolean.FALSE
+ case java.lang.Boolean.FALSE => java.lang.Boolean.TRUE
+ case null => java.lang.Boolean.TRUE
+ case _ => java.lang.Boolean.FALSE
+ }
+ }
}
def narrow[T](kind:Class[T], x:Iterable[Result[T, Throwable]],
f:java.util.List[String], q:String, p:java.lang.Integer, ps:java.lang.Integer,
o:java.util.List[String]) = {
@@ -466,7 +479,8 @@ case class BrokerResource() extends Reso
Success(rc)
} catch {
- case e:Throwable => Failure(e)
+ case e:Throwable =>
+ Failure(create_result(BAD_REQUEST, new ErrorDTO(e.getMessage)))
}
}
Added:
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/JaxrsExceptionMapper.scala
URL:
http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/JaxrsExceptionMapper.scala?rev=1221856&view=auto
==============================================================================
---
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/JaxrsExceptionMapper.scala
(added)
+++
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/JaxrsExceptionMapper.scala
Wed Dec 21 19:48:25 2011
@@ -0,0 +1,64 @@
+/**
+ * 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.activemq.apollo.web.resources
+
+import javax.ws.rs.core._
+import javax.ws.rs.ext._
+import javax.ws.rs._
+import core.Response.Status._
+import org.apache.activemq.apollo.dto.ErrorDTO
+import javax.servlet.http.HttpServletRequest
+
+/**
+ * <p>
+ * </p>
+ *
+ * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
+ */
+@Provider
+class JaxrsExceptionMapper extends ExceptionMapper[Throwable] {
+
+ @Context
+ var http_request: HttpServletRequest = null
+
+ def requested_uri = {
+ val query = http_request.getQueryString
+ http_request.getRequestURI + Option(query).map("?"+_).getOrElse("")
+ }
+
+ def toResponse(error: Throwable): Response = {
+
+ def response(status: Response.Status, msg: String) = {
+ val response = Response.status(status)
+ response.entity(new ErrorDTO(msg))
+ response.build
+ }
+
+ error match {
+ case ex:WebApplicationException =>
+ ex.getResponse.getStatus match {
+ case 404 =>
+ response(NOT_FOUND, "Resource not found: "+requested_uri)
+ case _ =>
+ ex.getResponse
+ }
+ case ex:Throwable =>
+ Resource.warn(ex, "HTTP request from '%s' for %s '%s' caused internal
server error: %s", http_request.getRemoteAddr, http_request.getMethod,
requested_uri, ex.toString);
+ response(INTERNAL_SERVER_ERROR, ex.toString)
+ }
+ }
+}
\ No newline at end of file
Modified:
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/Support.scala
URL:
http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/Support.scala?rev=1221856&r1=1221855&r2=1221856&view=diff
==============================================================================
---
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/Support.scala
(original)
+++
activemq/activemq-apollo/trunk/apollo-web/src/main/scala/org/apache/activemq/apollo/web/resources/Support.scala
Wed Dec 21 19:48:25 2011
@@ -23,7 +23,6 @@ import com.sun.jersey.api.view.ImplicitP
import Response._
import Response.Status._
import java.util.concurrent.TimeUnit
-import org.fusesource.hawtdispatch._
import org.fusesource.scalate.{NoValueSetException, RenderContext}
import com.sun.jersey.core.util.Base64
import javax.servlet.http.{HttpServletResponse, HttpServletRequest}
@@ -35,7 +34,7 @@ import org.apache.activemq.apollo.util._
import java.net.{InetSocketAddress, URI}
import java.security.cert.X509Certificate
-object Resource {
+object Resource extends Log {
val SECURITY_CONTEXT_ATTRIBUTE: String = classOf[SecurityContext].getName
val HEADER_WWW_AUTHENTICATE: String = "WWW-Authenticate"
@@ -60,7 +59,7 @@ object Resource {
*/
@ImplicitProduces(Array("text/html;qs=5"))
@Produces(Array("application/json", "application/xml","text/xml"))
-abstract class Resource(parent:Resource=null) extends Logging {
+abstract class Resource(parent:Resource=null) {
import Resource._
@Context
@@ -77,12 +76,16 @@ abstract class Resource(parent:Resource=
this.http_request = other.http_request
}
- def result(value:Status, message:Any=null):Nothing = {
+ def create_result(value: Response.Status, message: Any):
WebApplicationException = {
val response = Response.status(value)
- if( message!=null ) {
+ if (message != null) {
response.entity(message)
}
- throw new WebApplicationException(response.build)
+ new
WebApplicationException(response.build).fillInStackTrace().asInstanceOf[WebApplicationException]
+ }
+
+ def result(value:Status, message:Any=null):Nothing = {
+ throw create_result(value, message)
}
def result[T](uri:URI):T = {
Modified:
activemq/activemq-apollo/trunk/apollo-web/src/main/webapp/WEB-INF/web.xml
URL:
http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-web/src/main/webapp/WEB-INF/web.xml?rev=1221856&r1=1221855&r2=1221856&view=diff
==============================================================================
--- activemq/activemq-apollo/trunk/apollo-web/src/main/webapp/WEB-INF/web.xml
(original)
+++ activemq/activemq-apollo/trunk/apollo-web/src/main/webapp/WEB-INF/web.xml
Wed Dec 21 19:48:25 2011
@@ -53,6 +53,7 @@
<param-value>
org.apache.activemq.apollo.web.resources.RootResource,
org.apache.activemq.apollo.web.resources.JacksonJsonProvider
+ org.apache.activemq.apollo.web.resources.JaxrsExceptionMapper
org.fusesource.scalate.jersey.ScalateTemplateProvider
org.fusesource.scalate.jersey.ScalateTemplateProcessor
</param-value>
Modified:
activemq/activemq-apollo/trunk/apollo-website/src/documentation/management-api.md
URL:
http://svn.apache.org/viewvc/activemq/activemq-apollo/trunk/apollo-website/src/documentation/management-api.md?rev=1221856&r1=1221855&r2=1221856&view=diff
==============================================================================
---
activemq/activemq-apollo/trunk/apollo-website/src/documentation/management-api.md
(original)
+++
activemq/activemq-apollo/trunk/apollo-website/src/documentation/management-api.md
Wed Dec 21 19:48:25 2011
@@ -46,10 +46,22 @@ succeeded or failed. Expect the followi
* *`200`*: If a GET, PUT, or DELETE request succeeds.
* *`303`*: If a POST request succeeds.
-* *`404`*: If the resource cannot be found
-* *`401`*: If the user does not have access to the resource
+* *`400`*: The request could not be understood by the server due to malformed
syntax. The client SHOULD NOT repeat the request without modifications.
+* *`404`*: If the resource cannot be found.
+* *`401`*: If the user does not have access to the resource.
* *`50x`*: If an internal server error occurs while processing the request.
+If your get a *`40x`* or *`50x`* response code, the response message
+will contain a document describing the failure. For example, if we try
+to use invalid query syntax you would get:
+
+ $ curl -i -u "admin:password" `echo
'http://localhost:61680/broker/virtual-hosts/apollo-01/queues.json?f=id&q=foo(id=="foo")'`
+ HTTP/1.1 400 Bad Request
+ Content-Type: application/json
+ Transfer-Encoding: chunked
+
+ {"message":"Unable to find function (method): \"foo(java.lang.Boolean)\"
in any user-defined function handlers or the default function handler"}
+
### Working with Tabular Results
Many of the resource routes provided by the broker implement