Crowd : Getting all users removing duplicates in an application using Crowd REST java client

  1. //your crowd server settings
  2. String url = "http://auth.staging.company.com/crowd/";//crowd base url
  3. String applicationName = "appincrowd";
  4. String applicationPass = "apppassword";
  5. String groupName = "usergroup";//a user group under the app
  6. CrowdClient client = new RestCrowdClientFactory().newInstance(url, applicationName, applicationPass);
  7. //initialze crowd client

  1. //a single user can be more than one group,so let's remove duplicate users by using Set
  2.         Set<CrowdUser> allCrowdUsers = new HashSet<>();
  3.         List<String> newGroupList = client.searchGroupNames(NullRestrictionImpl.INSTANCE, 0, maxGroupSearchResult);
  4.         //get all users in a single list
  5.         for (String currGroupName : newGroupList) {
  6.             List<User> currGroupUsers = client.getUsersOfGroup(currGroupName, 0, maxUserSearchResult);
  7.             for (User grpUser : currGroupUsers) {
  8.                 allCrowdUsers.add(new CrowdUser(grpUser));
  9.             }
  10.         }
  11.         //traverse the all users list
  12.         for (User usr : allCrowdUsers) {
  13.             System.out.println(usr.getName() + " " + usr.getFirstName() + " " + usr.getLastName() + " (" + usr.getEmailAddress() + ")");
  14.         }
Output:
  1. //john cooper (cooper@company.com)
  2. //sam peters (sam@company.com)
  3. //...............................
  4. //Istiak Ahmad (iask@company.com)
Extended crowd user class:
CrowdUser.java:
  1. import com.atlassian.crowd.model.user.User;
  2. import com.atlassian.crowd.model.user.UserTemplate;
  3. /**
  4. *Extend user with overridden equals() and hashCode() method according to user email address so that we can build a collection(Set<>) with no different users with same email address
  5. *
  6. */
  7. public class CrowdUser extends UserTemplate {
  8.     public CrowdUser(User user) {
  9.         super(user);
  10.     }
  11.     /**
  12.      compare only user email address
  13.      */
  14.     @Override
  15.     public boolean equals(Object object) {
  16.         boolean result = false;
  17.         if (object == null || object.getClass() != getClass()) {
  18.             result = false;
  19.         } else {
  20.             CrowdUser muser = (CrowdUser) object;
  21.             if (this.getEmailAddress().compareTo(muser.getEmailAddress()) == 0) {
  22.                 result = true;
  23.             }
  24.         }
  25.         return result;
  26.     }
  27.     // only email address
  28.     @Override
  29.     public int hashCode() {
  30.         return this.getEmailAddress().hashCode();
  31.     }
  32.     
  33. }

Running maven web project with maven tomcat7 plugin

With maven tomcat7 plugin you won't have download,install and configure Tomcat server seperately.Maven can do it for you.Just run:
mvn tomcat7:run
with a tomcat7 maven plugin enabled project and browse to localhost:port/path
In your pom.xml simply put this plugin:
  1. <plugin>
  2. <groupId>org.apache.tomcat.maven</groupId>
  3. <artifactId>tomcat7-maven-plugin</artifactId>
  4. <version>2.0</version>
  5. <configuration>
  6. <server>tomcat-development-server</server>
  7. <port>9966</port> <!--make sure port doesnt conflict with others -->
  8. <path>/SpringMVC</path><!--http://localhost:9966/SpringMVC/ -->
  9. </configuration>
  10. </plugin>

Example spring maven hello world web project pom.xml:
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3.     <modelVersion>4.0.0</modelVersion>
  4.     <groupId>com.mohi</groupId>
  5.     <artifactId>SpringMVC</artifactId>
  6.     <packaging>war</packaging>
  7.     <version>1.0-SNAPSHOT</version>
  8.     <name>SpringMVC Maven Webapp</name>
  9.     <url>http://maven.apache.org</url>
  10.     <dependencies>
  11.         <dependency>
  12.             <groupId>org.springframework</groupId>
  13.             <artifactId>spring-webmvc</artifactId>
  14.             <version>4.0.3.RELEASE</version>
  15.         </dependency>
  16.     </dependencies>
  17.     <build>
  18.         <plugins>
  19.             <plugin>
  20.                 <groupId>org.apache.maven.plugins</groupId>
  21.                 <artifactId>maven-compiler-plugin</artifactId>
  22.                 <version>2.3.2</version>
  23.             </plugin>
  24.             <plugin>
  25.                 <groupId>org.apache.tomcat.maven</groupId>
  26.                 <artifactId>tomcat7-maven-plugin</artifactId>
  27.                 <version>2.0</version>
  28.                 <configuration>
  29.                     <server>tomcat-development-server</server>
  30.                     <port>9966</port>
  31.                     <path>/SpringMVC</path>
  32.                 </configuration>
  33.             </plugin>
  34.         </plugins>
  35.     </build>
  36. </project>

Sample code:
Here is a Spring Helloworld maven with tomcat7 plugin example project SpringHelloworldMavenTomcat7PluginExample.zip
Download ,unzip and run:
mvn clean install tomcat7:run
Browse to localhost:9966/SpringMVC/welcome

JSP Custom Tag Working Example

To write your own jsp tag:
Create a HelloTag class that extends SimpleTagSupport class.
Example:
  1. import javax.servlet.jsp.tagext.*;
  2. import javax.servlet.jsp.*;
  3. import java.io.*;
  4. public class HelloTag extends SimpleTagSupport {
  5. private String message;
  6. public void setMessage(String msg) {
  7. this.message = msg;
  8. }
  9. StringWriter sw = new StringWriter();
  10. public void doTag()
  11. throws JspException, IOException
  12. {
  13. if (message != null) {
  14. /* Use message from attribute */
  15. JspWriter out = getJspContext().getOut();
  16. out.println( message );
  17. }
  18. else {
  19.      System.out.println("msg is null");
  20. /* use message from the body */
  21. getJspBody().invoke(sw);
  22. getJspContext().getOut().println(sw.toString());
  23. }
  24. }
  25. }
Now create a  custom .tld file:
custom.tld:
-----------
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
  3. <taglib>
  4. <tlib-version>1.0</tlib-version>
  5. <jsp-version>2.0</jsp-version>
  6. <short-name>Example TLD with Body</short-name>
  7. <tag>
  8. <name>Hello</name>
  9. <tag-class>com.mohi.HelloTag</tag-class>
  10. <body-content>scriptless</body-content>
  11. <attribute>
  12. <name>message</name>
  13. </attribute>
  14. </tag>
  15. </taglib>
Usage:
Put the definitio on the top of your jsp page:
  1. <%@ taglib prefix="ex" uri="../custom.tld"%>//be aware of the path you are using it's relative!
Using your tag in jsp page body:
  1. <ex:Hello message="This is custom tag" />

Sample code:
Download JSPCustomTagExamplecode.zip
Unzip and run:
mvn clean install tomcat7:run
Browse to http://localhost:8080/SpringMVC/welcome

Spring HTML email example

Download spring html email example code from https://github.com/ahmedmohiduet/SpringHTMLEmailExample/

Unzip and run mvn clean install
Navigate to Spring-Mail.xml in src/main/resources folder.
Set your gmail email and password like this:
  1. <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
  2. <property name="host" value="smtp.gmail.com" />
  3. <property name="port" value="587" />
  4. <property name="username" value="yourgmailaddress@gmail.com" />
  5. <property name="password" value="gmailpassword" />
  6. <property name="javaMailProperties">
  7. <props>
  8. <prop key="mail.smtp.auth">true</prop>
  9. <prop key="mail.smtp.starttls.enable">true</prop>
  10. </props>
  11. </property>
  12. </bean>
Navigate to  src/main/java/com/mohi/common and run App.java:
A look of the email bean:
  1. public class HTMLMail
  2. {
  3.     private JavaMailSender mailSender;
  4.     public void setMailSender(JavaMailSender mailSender) {
  5.         this.mailSender = mailSender;
  6.     }
  7.     public void sendMail(String from, String to, String subject, String msg) {
  8.         try {
  9.             MimeMessage message = mailSender.createMimeMessage();
  10.             message.setSubject(subject);
  11.             MimeMessageHelper helper;
  12.             helper = new MimeMessageHelper(message, true);
  13.             helper.setFrom(from);
  14.             helper.setTo(to);
  15.             helper.setText(msg, true);
  16.             mailSender.send(message);
  17.         } catch (MessagingException ex) {
  18.             Logger.getLogger(HTMLMail.class.getName()).log(Level.SEVERE, null, ex);
  19.         }
  20.     }
  21. }