Expose via REST some useful info
authorChristos KK Loverdos <loverdos@gmail.com>
Fri, 8 Jun 2012 12:52:33 +0000 (15:52 +0300)
committerChristos KK Loverdos <loverdos@gmail.com>
Fri, 8 Jun 2012 12:52:33 +0000 (15:52 +0300)
src/main/scala/gr/grnet/aquarium/Aquarium.scala
src/main/scala/gr/grnet/aquarium/Main.scala
src/main/scala/gr/grnet/aquarium/ResourceLocator.scala
src/main/scala/gr/grnet/aquarium/actor/service/rest/RESTActor.scala
src/main/scala/gr/grnet/aquarium/actor/service/rest/RESTPaths.scala

index 1c6b64b..52a3749 100644 (file)
@@ -326,8 +326,9 @@ final class Aquarium(val props: Props) extends Lifecycle with Loggable { aquariu
     }
 
     logger.info("CONF_HERE =  {}", HERE)
-    logger.info("{} = {}", ResourceLocator.ResourceNames.LOGBACK_XML, LOGBACK_XML)
     logger.info("{} = {}", ResourceLocator.ResourceNames.AQUARIUM_PROPERTIES, ResourceLocator.Resources.AquariumPropertiesResource)
+    logger.info("{} = {}", ResourceLocator.ResourceNames.LOGBACK_XML, ResourceLocator.Resources.LogbackXMLResource)
+    logger.info("{} = {}", ResourceLocator.ResourceNames.POLICY_YAML, ResourceLocator.Resources.PolicyYAMLResource)
   }
 
   private[this] def addShutdownHooks(): Unit = {
index 323a163..eae67a8 100644 (file)
@@ -53,19 +53,13 @@ object Main extends LazyLoggable {
     // Make sure AQUARIUM_HOME is configured, since it is used in logback.xml
     assert(ResourceLocator.Homes.Folders.AquariumHome.isDirectory)
 
-    ResourceLocator.LOGBACK_XML match {
-      case Just(resource) ⇒
-        val f = LoggerFactory.getILoggerFactory
-        f match {
-          case context: LoggerContext ⇒
-            val joran = new JoranConfigurator
-            joran.setContext(context)
-            context.reset()
-            joran.doConfigure(resource.url)
-        }
-
-      case _ ⇒
-        // Otherwise it will be configured in what way logback does it automatically
+    val f = LoggerFactory.getILoggerFactory
+    f match {
+      case context: LoggerContext ⇒
+        val joran = new JoranConfigurator
+        joran.setContext(context)
+        context.reset()
+        joran.doConfigure(ResourceLocator.Resources.LogbackXMLResource.url)
     }
   }
 
index 378dda0..4f1f028 100644 (file)
@@ -203,11 +203,6 @@ object ResourceLocator {
    */
   final lazy val HERE = justForSure(getResource(".")).get.url.toExternalForm
 
-  /**
-   * This exists in order to have a feeling of where we are.
-   */
-  final lazy val LOGBACK_XML = getResource(ResourceNames.LOGBACK_XML)
-
   final object Resources {
     final lazy val AquariumPropertiesResource = {
       ResourceLocator.SysProps.AquariumPropertiesPath.value match {
@@ -239,8 +234,38 @@ object ResourceLocator {
           }
       }
     }
-  }
 
+    final lazy val LogbackXMLResource = {
+      getResource(ResourceNames.LOGBACK_XML) match {
+        case Just(logbackXML) ⇒
+          logbackXML
+
+        case NoVal ⇒
+          throw new AquariumInternalError(
+            "Could not find %s".format(ResourceLocator.ResourceNames.LOGBACK_XML))
+
+        case Failed(e) ⇒
+          throw new AquariumInternalError(
+            "Could not find %s".format(ResourceLocator.ResourceNames.LOGBACK_XML), e)
+
+      }
+    }
+
+    final lazy val PolicyYAMLResource = {
+      ResourceLocator.getResource(ResourceLocator.ResourceNames.POLICY_YAML) match {
+        case Just(policyYAML) ⇒
+          policyYAML
+
+        case NoVal ⇒
+          throw new AquariumInternalError(
+            "Could not find %s".format(ResourceLocator.ResourceNames.POLICY_YAML))
+
+        case Failed(e) ⇒
+          throw new AquariumInternalError(
+            "Could not find %s".format(ResourceLocator.ResourceNames.POLICY_YAML), e)
+      }
+    }
+  }
 
   def getResource(what: String): Maybe[StreamResource] = {
     ResourceContexts.MasterResourceContext.getResource(what)
index 174aa5f..d87e055 100644 (file)
@@ -40,15 +40,16 @@ package rest
 import cc.spray.can.HttpMethods.GET
 import cc.spray.can._
 import gr.grnet.aquarium.util.Loggable
-import gr.grnet.aquarium.Aquarium
 import akka.actor.Actor
 import gr.grnet.aquarium.actor.{RESTRole, RoleableActor, RouterRole}
-import RESTPaths.{UserBalancePath, UserStatePath, AdminPingAll}
-import com.ckkloverdos.maybe.{NoVal, Just}
+import RESTPaths._
 import gr.grnet.aquarium.util.date.TimeHelpers
 import org.joda.time.format.ISODateTimeFormat
 import gr.grnet.aquarium.actor.message.admin.PingAllRequest
 import gr.grnet.aquarium.actor.message.{RouterResponseMessage, GetUserStateRequest, RouterRequestMessage, ActorMessage, GetUserBalanceRequest}
+import gr.grnet.aquarium.{ResourceLocator, Aquarium}
+import com.ckkloverdos.resource.StreamResource
+import com.ckkloverdos.maybe.{Failed, NoVal, Just}
 
 /**
  * Spray-based REST service. This is the outer-world's interface to Aquarium functionality.
@@ -70,6 +71,24 @@ class RESTActor private(_id: String) extends RoleableActor with Loggable {
     )
   }
 
+  private def resourceInfoResponse(
+      responder: RequestResponder,
+      resource: StreamResource,
+      contentType: String
+  ): Unit = {
+
+    val fmt = (body: String) ⇒ "%s\n\n%s".format(resource.url, body)
+    val res = resource.mapString(body ⇒ responder.complete(stringResponse(200, fmt(body), contentType)))
+
+    res match {
+      case Failed(e) ⇒
+        logger.error("While serving %s".format(resource), e)
+
+      case _ ⇒
+
+    }
+  }
+
   private def stringResponse200(stringBody: String, contentType: String = "application/json"): HttpResponse = {
     stringResponse(200, stringBody, contentType)
   }
@@ -131,6 +150,15 @@ class RESTActor private(_id: String) extends RoleableActor with Loggable {
               responder.complete(stringResponse(403, "Forbidden!", "text/plain"))
           }
 
+        case ResourcesAquariumProperties() ⇒
+          resourceInfoResponse(responder, ResourceLocator.Resources.AquariumPropertiesResource, "text/plain")
+
+        case ResourcesLogbackXML() ⇒
+          resourceInfoResponse(responder, ResourceLocator.Resources.LogbackXMLResource, "text/plain")
+
+        case ResourcesPolicyYAML() ⇒
+          resourceInfoResponse(responder, ResourceLocator.Resources.PolicyYAMLResource, "text/plain")
+
         case _ ⇒
           responder.complete(stringResponse(404, "Unknown resource!", "text/plain"))
       }
index e84de40..fad277b 100644 (file)
@@ -44,6 +44,13 @@ package rest
  * @author Christos KK Loverdos <loverdos@gmail.com>.
  */
 object RESTPaths {
+  final val ResourcesLogbackXML = "/resources/logback\\.xml".r
+
+  final val ResourcesAquariumProperties = "/resources/aquarium\\.properties".r
+
+  final val ResourcesPolicyYAML = "/resources/policy\\.yaml".r
+
+  final val RolesAgreementsMap = "/resources/roles-agreements\\.map".r
   /**
    * Use this URI path to query for the user balance. The parenthesized regular expression part
    * represents the user ID.