killbill-memoizeit

Details

diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SessionJson.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SessionJson.java
new file mode 100644
index 0000000..1665430
--- /dev/null
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SessionJson.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright 2010-2014 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.jaxrs.json;
+
+import org.apache.shiro.session.Session;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class SessionJson {
+
+    private final String id;
+    private final DateTime startDate;
+    private final DateTime lastAccessDate;
+    private final Long timeout;
+    private final String host;
+
+    @JsonCreator
+    public SessionJson(@JsonProperty("id") final String id,
+                       @JsonProperty("startDate") final DateTime startDate,
+                       @JsonProperty("lastAccessDate") final DateTime lastAccessDate,
+                       @JsonProperty("timeout") final Long timeout,
+                       @JsonProperty("host") final String host) {
+        this.id = id;
+        this.startDate = startDate;
+        this.lastAccessDate = lastAccessDate;
+        this.timeout = timeout;
+        this.host = host;
+    }
+
+    public SessionJson(final Session session) {
+        this.id = session.getId() == null ? null : session.getId().toString();
+        this.startDate = session.getStartTimestamp() == null ? null : new DateTime(session.getStartTimestamp(), DateTimeZone.UTC);
+        this.lastAccessDate = session.getLastAccessTime() == null ? null : new DateTime(session.getLastAccessTime(), DateTimeZone.UTC);
+        this.timeout = session.getTimeout();
+        this.host = session.getHost();
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public DateTime getStartDate() {
+        return startDate;
+    }
+
+    public DateTime getLastAccessDate() {
+        return lastAccessDate;
+    }
+
+    public Long getTimeout() {
+        return timeout;
+    }
+
+    public String getHost() {
+        return host;
+    }
+
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder("SessionJson{");
+        sb.append("id='").append(id).append('\'');
+        sb.append(", startDate=").append(startDate);
+        sb.append(", lastAccessDate=").append(lastAccessDate);
+        sb.append(", timeout=").append(timeout);
+        sb.append(", host='").append(host).append('\'');
+        sb.append('}');
+        return sb.toString();
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        final SessionJson that = (SessionJson) o;
+
+        if (host != null ? !host.equals(that.host) : that.host != null) {
+            return false;
+        }
+        if (id != null ? !id.equals(that.id) : that.id != null) {
+            return false;
+        }
+        if (lastAccessDate != null ? !lastAccessDate.equals(that.lastAccessDate) : that.lastAccessDate != null) {
+            return false;
+        }
+        if (startDate != null ? !startDate.equals(that.startDate) : that.startDate != null) {
+            return false;
+        }
+        if (timeout != null ? !timeout.equals(that.timeout) : that.timeout != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = id != null ? id.hashCode() : 0;
+        result = 31 * result + (startDate != null ? startDate.hashCode() : 0);
+        result = 31 * result + (lastAccessDate != null ? lastAccessDate.hashCode() : 0);
+        result = 31 * result + (timeout != null ? timeout.hashCode() : 0);
+        result = 31 * result + (host != null ? host.hashCode() : 0);
+        return result;
+    }
+}
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SubjectJson.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SubjectJson.java
new file mode 100644
index 0000000..d535d0a
--- /dev/null
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/json/SubjectJson.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2010-2014 Ning, Inc.
+ *
+ * Ning 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 com.ning.billing.jaxrs.json;
+
+import javax.annotation.Nullable;
+
+import org.apache.shiro.session.Session;
+import org.apache.shiro.subject.Subject;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+public class SubjectJson {
+
+    private final String principal;
+    private final Boolean isAuthenticated;
+    private final Boolean isRemembered;
+    private final SessionJson session;
+
+    @JsonCreator
+    public SubjectJson(@JsonProperty("principal") final String principal,
+                       @JsonProperty("isAuthenticated") final Boolean isAuthenticated,
+                       @JsonProperty("isRemembered") final Boolean isRemembered,
+                       @JsonProperty("session") @Nullable final SessionJson session) {
+        this.principal = principal;
+        this.isAuthenticated = isAuthenticated;
+        this.isRemembered = isRemembered;
+        this.session = session;
+    }
+
+    public SubjectJson(final Subject subject) {
+        this.principal = subject.getPrincipal() == null ? null : subject.getPrincipal().toString();
+        this.isAuthenticated = subject.isAuthenticated();
+        this.isRemembered = subject.isRemembered();
+        final Session subjectSession = subject.getSession(false);
+        this.session = subjectSession == null ? null : new SessionJson(subjectSession);
+    }
+
+    public String getPrincipal() {
+        return principal;
+    }
+
+    public Boolean getIsAuthenticated() {
+        return isAuthenticated;
+    }
+
+    public Boolean getIsRemembered() {
+        return isRemembered;
+    }
+
+    public SessionJson getSession() {
+        return session;
+    }
+
+    @Override
+    public String toString() {
+        final StringBuilder sb = new StringBuilder("SubjectJson{");
+        sb.append("principal='").append(principal).append('\'');
+        sb.append(", isAuthenticated=").append(isAuthenticated);
+        sb.append(", isRemembered=").append(isRemembered);
+        sb.append(", session=").append(session);
+        sb.append('}');
+        return sb.toString();
+    }
+
+    @Override
+    public boolean equals(final Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        final SubjectJson that = (SubjectJson) o;
+
+        if (isAuthenticated != null ? !isAuthenticated.equals(that.isAuthenticated) : that.isAuthenticated != null) {
+            return false;
+        }
+        if (isRemembered != null ? !isRemembered.equals(that.isRemembered) : that.isRemembered != null) {
+            return false;
+        }
+        if (principal != null ? !principal.equals(that.principal) : that.principal != null) {
+            return false;
+        }
+        if (session != null ? !session.equals(that.session) : that.session != null) {
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = principal != null ? principal.hashCode() : 0;
+        result = 31 * result + (isAuthenticated != null ? isAuthenticated.hashCode() : 0);
+        result = 31 * result + (isRemembered != null ? isRemembered.hashCode() : 0);
+        result = 31 * result + (session != null ? session.hashCode() : 0);
+        return result;
+    }
+}
diff --git a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/SecurityResource.java b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/SecurityResource.java
index 133d632..db1934e 100644
--- a/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/SecurityResource.java
+++ b/jaxrs/src/main/java/com/ning/billing/jaxrs/resources/SecurityResource.java
@@ -27,8 +27,12 @@ import javax.ws.rs.Produces;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 
+import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.subject.Subject;
+
 import com.ning.billing.account.api.AccountUserApi;
 import com.ning.billing.clock.Clock;
+import com.ning.billing.jaxrs.json.SubjectJson;
 import com.ning.billing.jaxrs.util.Context;
 import com.ning.billing.jaxrs.util.JaxrsUriBuilder;
 import com.ning.billing.security.Permission;
@@ -66,10 +70,18 @@ public class SecurityResource extends JaxRsResourceBase {
     @GET
     @Path("/permissions")
     @Produces(APPLICATION_JSON)
-    public Response getCurrentUserPermissions(@javax.ws.rs.core.Context final HttpServletRequest request)  {
+    public Response getCurrentUserPermissions(@javax.ws.rs.core.Context final HttpServletRequest request) {
         final Set<Permission> permissions = securityApi.getCurrentUserPermissions(context.createContext(request));
         final List<String> json = ImmutableList.<String>copyOf(Iterables.<Permission, String>transform(permissions, Functions.toStringFunction()));
         return Response.status(Status.OK).entity(json).build();
     }
 
-}
+    @GET
+    @Path("/subject")
+    @Produces(APPLICATION_JSON)
+    public Response getCurrentUserSubject(@javax.ws.rs.core.Context final HttpServletRequest request) {
+        final Subject subject = SecurityUtils.getSubject();
+        final SubjectJson subjectJson = new SubjectJson(subject);
+        return Response.status(Status.OK).entity(subjectJson).build();
+    }
+}
\ No newline at end of file