Blob Blame History Raw
From ebe79a4db05662091bd15efc860ef6923beff2aa Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Thu, 20 Oct 2016 00:54:47 +0200
Subject: [PATCH] Updated AccountInfo.

The AccountInfo has been changed to extend the ResourceMessage
such that it can be used to pass the list of accessible
components as an attribute.

https://fedorahosted.org/pki/ticket/2523
(cherry picked from commit 6d5d15edebeb1ba113e4f3d5b2bb1ba93a92ce1d)
(cherry picked from commit 388e443b99999dd2aa0832aaed0cdf43ee260b81)
---
 .../com/netscape/certsrv/account/AccountInfo.java  |  8 ++--
 .../com/netscape/certsrv/base/ResourceMessage.java | 11 +++++-
 .../org/dogtagpki/server/rest/AccountService.java  | 46 ++++++++--------------
 3 files changed, 31 insertions(+), 34 deletions(-)

diff --git a/base/common/src/com/netscape/certsrv/account/AccountInfo.java b/base/common/src/com/netscape/certsrv/account/AccountInfo.java
index 7943d22..3310bf0 100644
--- a/base/common/src/com/netscape/certsrv/account/AccountInfo.java
+++ b/base/common/src/com/netscape/certsrv/account/AccountInfo.java
@@ -33,11 +33,13 @@ import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 
+import com.netscape.certsrv.base.ResourceMessage;
+
 /**
  * @author Endi S. Dewata
  */
 @XmlRootElement(name="Account")
-public class AccountInfo {
+public class AccountInfo extends ResourceMessage {
 
     public static Marshaller marshaller;
     public static Unmarshaller unmarshaller;
@@ -98,7 +100,7 @@ public class AccountInfo {
     @Override
     public int hashCode() {
         final int prime = 31;
-        int result = 1;
+        int result = super.hashCode();
         result = prime * result + ((email == null) ? 0 : email.hashCode());
         result = prime * result + ((fullName == null) ? 0 : fullName.hashCode());
         result = prime * result + ((id == null) ? 0 : id.hashCode());
@@ -110,7 +112,7 @@ public class AccountInfo {
     public boolean equals(Object obj) {
         if (this == obj)
             return true;
-        if (obj == null)
+        if (!super.equals(obj))
             return false;
         if (getClass() != obj.getClass())
             return false;
diff --git a/base/common/src/com/netscape/certsrv/base/ResourceMessage.java b/base/common/src/com/netscape/certsrv/base/ResourceMessage.java
index 1214b45..85d0d07 100644
--- a/base/common/src/com/netscape/certsrv/base/ResourceMessage.java
+++ b/base/common/src/com/netscape/certsrv/base/ResourceMessage.java
@@ -26,6 +26,7 @@ import javax.xml.bind.annotation.XmlValue;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 
+import com.netscape.certsrv.account.AccountInfo;
 import com.netscape.certsrv.key.AsymKeyGenerationRequest;
 import com.netscape.certsrv.key.KeyArchivalRequest;
 import com.netscape.certsrv.key.KeyRecoveryRequest;
@@ -35,8 +36,14 @@ import com.netscape.certsrv.key.SymKeyGenerationRequest;
  * @author Ade Lee
  */
 @XmlRootElement(name = "ResourceMessage")
-@XmlSeeAlso({ KeyArchivalRequest.class, KeyRecoveryRequest.class, SymKeyGenerationRequest.class,
-        PKIException.Data.class, AsymKeyGenerationRequest.class })
+@XmlSeeAlso({
+    AccountInfo.class,
+    KeyArchivalRequest.class,
+    KeyRecoveryRequest.class,
+    SymKeyGenerationRequest.class,
+    PKIException.Data.class,
+    AsymKeyGenerationRequest.class
+})
 @XmlAccessorType(XmlAccessType.NONE)
 public class ResourceMessage {
 
diff --git a/base/server/cms/src/org/dogtagpki/server/rest/AccountService.java b/base/server/cms/src/org/dogtagpki/server/rest/AccountService.java
index 827e99e..673db45 100644
--- a/base/server/cms/src/org/dogtagpki/server/rest/AccountService.java
+++ b/base/server/cms/src/org/dogtagpki/server/rest/AccountService.java
@@ -21,13 +21,8 @@ package org.dogtagpki.server.rest;
 import java.security.Principal;
 import java.util.Arrays;
 
-import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
-import javax.ws.rs.core.Context;
-import javax.ws.rs.core.HttpHeaders;
-import javax.ws.rs.core.Request;
 import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriInfo;
 
 import org.apache.catalina.realm.GenericPrincipal;
 import org.apache.commons.lang.StringUtils;
@@ -43,47 +38,40 @@ import com.netscape.cms.servlet.base.PKIService;
  */
 public class AccountService extends PKIService implements AccountResource {
 
-    @Context
-    private UriInfo uriInfo;
-
-    @Context
-    private HttpHeaders headers;
-
-    @Context
-    private Request request;
-
-    @Context
-    private HttpServletRequest servletRequest;
-
-    @Override
-    public Response login() {
-        HttpSession session = servletRequest.getSession();
-        System.out.println("Creating session "+session.getId());
-
+    protected AccountInfo createAccountInfo() {
         Principal principal = servletRequest.getUserPrincipal();
-        System.out.println("Principal: "+principal);
+        System.out.println("Principal: " + principal);
 
-        AccountInfo response = new AccountInfo();
+        AccountInfo accountInfo = new AccountInfo();
         String name = principal.getName();
-        response.setID(name);
+        accountInfo.setID(name);
 
         if (principal instanceof PKIPrincipal) {
             PKIPrincipal pkiPrincipal = (PKIPrincipal)principal;
             IUser user = pkiPrincipal.getUser();
 
             String fullName = user.getFullName();
-            if (!StringUtils.isEmpty(fullName)) response.setFullName(fullName);
+            if (!StringUtils.isEmpty(fullName)) accountInfo.setFullName(fullName);
 
             String email = user.getEmail();
-            if (!StringUtils.isEmpty(email)) response.setEmail(email);
+            if (!StringUtils.isEmpty(email)) accountInfo.setEmail(email);
         }
 
         if (principal instanceof GenericPrincipal) {
             String[] roles = ((GenericPrincipal) principal).getRoles();
-            response.setRoles(Arrays.asList(roles));
+            accountInfo.setRoles(Arrays.asList(roles));
         }
 
-        return createOKResponse(response);
+        return accountInfo;
+    }
+
+    @Override
+    public Response login() {
+        HttpSession session = servletRequest.getSession();
+        System.out.println("Creating session " + session.getId());
+
+        AccountInfo accountInfo = createAccountInfo();
+        return createOKResponse(accountInfo);
     }
 
     @Override
-- 
1.8.3.1