Display URL support for User Profile Adaptor
Code Review https://codereview.appspot.com/13191044/
diff --git a/src/com/google/enterprise/adaptor/sharepoint/SharePointUserProfileAdaptor.java b/src/com/google/enterprise/adaptor/sharepoint/SharePointUserProfileAdaptor.java
index 1bfd1f0..d00f6d6 100644
--- a/src/com/google/enterprise/adaptor/sharepoint/SharePointUserProfileAdaptor.java
+++ b/src/com/google/enterprise/adaptor/sharepoint/SharePointUserProfileAdaptor.java
@@ -42,6 +42,7 @@
 import java.io.UnsupportedEncodingException;
 import java.net.Authenticator;
 import java.net.PasswordAuthentication;
+import java.net.URI;
 import java.net.URL;
 import java.net.URLEncoder;
 import java.nio.charset.Charset;
@@ -123,6 +124,7 @@
       Logger.getLogger(SharePointUserProfileAdaptor.class.getName());
 
   private String virtualServer;
+  private String mySiteHost;
   private NtlmAuthenticator ntlmAuthenticator;
   private final UserProfileServiceFactory userProfileServiceFactory;
 
@@ -168,6 +170,7 @@
     config.addKey("sharepoint.password", null);
     config.addKey("profile.setacl", "true");
     config.addKey("adaptor.namespace", "Default");
+    config.addKey("profile.mysitehost", "");
   }
 
   @Override
@@ -188,6 +191,18 @@
     log.log(Level.CONFIG, "Username: {0}", username);
     log.log(Level.CONFIG, "setAcl: {0}", setAcl);
     log.log(Level.CONFIG, "Namespace: {0}", namespace);
+    
+    mySiteHost = config.getValue("profile.mysitehost");
+    log.log(Level.CONFIG, "mySiteHost: {0}", mySiteHost);
+    if (mySiteHost.isEmpty()) {
+      log.log(Level.WARNING, "My site host is not specified."
+          + " Using virtual server url as My site host.");
+      mySiteHost = virtualServer;
+    }
+    
+    if (mySiteHost.endsWith("/")) {
+      mySiteHost = mySiteHost.substring(0, mySiteHost.length() - 1);
+    }
 
     ntlmAuthenticator = new NtlmAuthenticator(username, password);
     // Unfortunately, this is a JVM-wide modification.
@@ -296,18 +311,22 @@
           .address(endpoint).build();
       EndpointReference endpointChangeRef = new W3CEndpointReferenceBuilder()
           .address(endpointChangeService).build();
+      UserProfileServiceSoap inUserProfileServiceSoap 
+          = userProfileServiceSoap.getPort(
+              endpointRef, UserProfileServiceSoap.class);       
+      UserProfileChangeServiceSoap inUserProfileChangeServiceSoap 
+          = userProfileChangeServiceSoap.getPort(
+              endpointChangeRef, UserProfileChangeServiceSoap.class);
       // JAX-WS RT 2.1.4 doesn't handle headers correctly and always assumes the
       // list contains precisely one entry, so we work around it here.
       if (!cookies.isEmpty()) {
         addFormsAuthenticationCookies(
-            (BindingProvider) userProfileServiceSoap, cookies);
+            (BindingProvider) inUserProfileServiceSoap, cookies);
         addFormsAuthenticationCookies(
-            (BindingProvider) userProfileChangeServiceSoap, cookies);
+            (BindingProvider) inUserProfileChangeServiceSoap, cookies);
       }
-      return new SharePointUserProfileServiceWS(userProfileServiceSoap.
-          getPort(endpointRef, UserProfileServiceSoap.class),
-          userProfileChangeServiceSoap.getPort(endpointChangeRef,
-              UserProfileChangeServiceSoap.class));
+      return new SharePointUserProfileServiceWS(inUserProfileServiceSoap,
+          inUserProfileChangeServiceSoap);
     }
     
     private void addFormsAuthenticationCookies(BindingProvider port, 
@@ -525,6 +544,11 @@
           response.addMetadata(GSA_PROPNAME_COLLEAGUES, colleaguesXml);
         }
       }
+
+      String displayUrl = mySiteHost + "/person.aspx?accountname=" 
+          + URLEncoder.encode(userName, "UTF-8");
+      response.setDisplayUrl(URI.create(displayUrl));
+      
       String userProfileTitle = getUserProfilePropertySingleValue(
           userProfileProperties, PROFILE_PREFERRED_NAME_PROPERTY);
       if (userProfileTitle == null) {
diff --git a/test/com/google/enterprise/adaptor/sharepoint/SharePointUserProfileAdaptorTest.java b/test/com/google/enterprise/adaptor/sharepoint/SharePointUserProfileAdaptorTest.java
index 513bb06..a997dea 100644
--- a/test/com/google/enterprise/adaptor/sharepoint/SharePointUserProfileAdaptorTest.java
+++ b/test/com/google/enterprise/adaptor/sharepoint/SharePointUserProfileAdaptorTest.java
@@ -34,6 +34,7 @@
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.net.URI;
 import java.net.URLDecoder;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
@@ -146,7 +147,7 @@
     ArrayOfPropertyData profile = new ArrayOfPropertyData();
     poulateProfileProperties(profile,
         SharePointUserProfileAdaptor.PROFILE_ACCOUNTNAME_PROPERTY,
-        new String[] {"user1"});
+        new String[] {"domain\\user1"});
     poulateProfileProperties(profile,
         SharePointUserProfileAdaptor.PROFILE_PREFERRED_NAME_PROPERTY,
         new String[] {"First & Last"});
@@ -182,7 +183,8 @@
     cPrivate.setName("Private Colleague");
     colleaguesData.getContactData().add(cPrivate);
 
-    serviceFactory.addUserProfileToCollection(1, 2, "user1", profile, colleaguesData);
+    serviceFactory.addUserProfileToCollection(1, 2, "domain\\user1",
+        profile, colleaguesData);
     adaptor = new SharePointUserProfileAdaptor(serviceFactory);
     config.overrideKey("adaptor.namespace", "ns1");
 
@@ -191,7 +193,8 @@
 
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     GetContentsRequest request = new GetContentsRequest(
-        new DocId(SharePointUserProfileAdaptor.SOCIAL_ID_PREFIX + "user1"));
+        new DocId(SharePointUserProfileAdaptor.SOCIAL_ID_PREFIX 
+          + "domain\\user1"));
     GetContentsResponse response = new GetContentsResponse(baos);
     adaptor.getDocContent(request, response);
     
@@ -201,7 +204,7 @@
     assertEquals(golden, responseString);
 
     assertFalse(response.isNotFound());
-    assertEquals("user1", response.getMetadata().getOneValue(
+    assertEquals("domain\\user1", response.getMetadata().getOneValue(
         "google_social_user_accountname"));
     assertEquals("Value1", response.getMetadata().getOneValue(
         "SP Single Value Property"));
@@ -240,6 +243,9 @@
     String isInWorkGroup
         = pubContact.getAttributes().getNamedItem("gsa:isinworkinggroup").getNodeValue();
     assertEquals("true", URLDecoder.decode(isInWorkGroup, "UTF-8"));
+    
+    assertEquals(URI.create("http://sharepoint.example.com/person.aspx?"
+        + "accountname=domain%5Cuser1"), response.getDisplayUrl());