Using WebLogic Server Embedded LDAP

LDAP is widely used in webapplication for authorization .we have to set embedded weblogic LDAP server for setting up development enviroment.
First question we have – What is LDAP ?

LDAP is Lightweight Directory Access Protocol.LDAP is a protocol for accessing a directory. A directory contains objects; generally those related to users, groups, computers, printers and so on; company structure information (although frankly you can extend it and store anything in there).
LDAP gives you query methods to add, update and remove objects within a directory (and a bunch more, but those are the central ones).

-Connect to a directory (with varying levels of security)
-Read the entries in a directory
-Write entries in a directory
-Search a directory
-Rename entries in a directory
-Delete entries in a directory

IF you are reading about LDAP the you should also know –

what is directory
A directory is a type of hierarchical database. It is made up of entries, that have a globally unique name, and contain attributes that are named collections of data values. directories are optimised for fast look up, they have a strong security model and they scale well. Because they are a tree structure, different parts of the tree can be maintained by different directories and different administrators. The tree data structure also fits some cases much better than a relational database. (BTW – If you’re familiar with relational databases, ldap is conceptually similar to SQL.)

CONNECT TO WEBLOGIC SERVER EMBEDDED LDAP USING LDAP BROWSER-

WebLogic Server includes an embedded LDAP server that acts as the default security provider data store for the Default Authentication, Authorization, Credential Mapping, and Role Mapping providers.embedded LDAP server contains user, group, group membership, security role, security policy, and credential map information. By default, each WebLogic Server domain has an embedded LDAP server configured with the default values set for each type of information. The Default Authentication, Authorization, Credential Mapping, and Role Mapping providers use the embedded LDAP server as their data store.

In the WebLogic Server Administration Console, change the credential for the embedded LDAP server:

Expand Domain > Security > Embedded LDAP.

– In the Credential field, enter the new credential.

– In the Confirm Credential field, enter the new credential again.

– Click Save.

– Restart WebLogic Server.

Now we will be needing the LDAP browser to access it.There are multiple LDAP browser like

– OpenLDap -http://www.openldap.org/
– jxplorer – jxplorer.org

Following is connection parameter as below –

Hostname: Hostname of the WebLogic Server.
Port: WebLogic Admin Server port.
Base DN: This is the WebLogic domain name.
User DN: By default the Admin user DN is cn=Admin.
Password: Admin user password

1 In the LDAP browser, configure a new connection in the LDAP browser:

-Select the QuickConnect tab.

-Set the host field to localhost.

-Set the port field to 7001 (7002 if SSL is being used).

Set the Base DN field to dc=mydomain where mydomain is the name of the WebLogic Server domain you are using.

– Uncheck the Anonymous Bind option.

– Set the User DN field to cn=Admin.

– Set the Password field to the credential you specified in Step 2.

2 Click the new connection.

– Use the LDAP browser to navigate the hierarchy of the embedded LDAP server.

Now You can create users/groups .

Hope it helps ..happy coding with Vinay in techartifact ……

Embedded Server – Tomcat 7

What is an Embedded Server?

Traditionally, java web applications have been seen as WAR files deployed on servlet containers and application servers.
Simultaneously another school of thought advocates deploying a server with an application; pioneered by Jetty. Tomcat too comes in this flavour – as embedded tomcat. In an embedded server application, the application is deployed and run as a JAR. This JAR file launches a main() method which in turn starts the server and deploys itself on it. With such an application, we eliminate dependency on having a server. Any machine that can run java, can act as a server.What advantages such a server have? None. The only advantage we have is that we can use it to develop small applications. Applications that can be run on machine that have just a JRE. For example, say in my uncle’s medicine shop. There is no setup cost involved – other than the machine itself.

In this article

In this article, we shall discuss about
  1. Softwares involved
  2. Project setup using maven
  3. Embedding tomcat server

Softwares involved

  1. JDK – preferably the latest
  2. Maven – preferably the latest
  3. An editor to write java classes.

You should also be connected to internet – to allow maven download its dependencies – and I bet there are many.

Project setup using Maven

We shall be creating a web archieve project – inline with J2EE specification. But we shall also be using maven. So the folder structure will follow maven standard, and not the structure that we see in eclipse projects.
Create the following depicted folder structure
  • The name of project is ‘SampleProject’.
  • There is an src folder that houses 2 folders – ‘main’ and ‘test’.
  • The ‘test’ folder is used to write test scripts. We won’t be doing that.
  • The ‘src’ folder is where we keep java source and also the web appllication files. So it has 2 children – ‘java’ for source code and ‘webapp’ for the web application resources. See the ‘WEB-INF’ folder that houses web.xml file.
Next create a pom.xml file in ‘SampleProject’ folder. And write the following in it.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.heroku.sample</groupId>
<artifactId>embeddedTomcatSample</artifactId>
<version>1.0-SNAPSHOT</version>
<name>embeddedTomcatSample Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>7.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>7.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>7.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>7.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>7.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>7.0.22</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
<build>
<finalName>embeddedTomcatSample</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<assembleDirectory>target</assembleDirectory>
<programs>
<program>
<mainClass>com.ego.apps.Launch</mainClass>
<name>webapp</name>
</program>
</programs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>assemble</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Note – the dependencies section list out the JARs needed for embedded tomcat.

Now we need to tell maven to download these dependencies – so that we can continue with our application. Navigate to the SampleProject folder and issue command

mvn install

This would install all the dependencies required.

Now, go ahead and create a servlet. We created following

package com.ego.apps.servlets;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
    {
    response.getOutputStream().print("Hello World!");
    }
catch (IOException e)
    {
    }
}
}

And edit the web.xml in WEB-INF as

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>com.ego.apps.servlets.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

</web-app>

This completes our web application setup by issuing final command

mvn clean install

Embedding tomcat server

We have our web application ready. It can be archieved as a WAR and deployed on any server.
But no, we are smart and will package our server with out application. So create a java file as given below
package com.ego.apps;

import java.io.File;

import org.apache.catalina.startup.Tomcat;
import org.apache.log4j.Logger;

public class Launch {
public static void main(String[] args) throws Exception {
Logger logger = Logger.getLogger(Launch.class);
String webappDirLocation = "src/main/webapp/";
Tomcat tomcat = new Tomcat();

// Look for that variable and default to 8080 if it isn't there.
String webPort = System.getenv("PORT");
if (webPort == null || webPort.isEmpty()) {
webPort = "8080";
}
logger.info("Application port set to: " + webPort);
tomcat.setPort(Integer.valueOf(webPort));

tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
logger.info("configuring app with basedir: "
+ new File("./" + webappDirLocation).getAbsolutePath());

tomcat.start();
logger.info("Application started.");
tomcat.getServer().await();
}
}
 If you go through the main() method of the class, you will figure out that it instantiates tomcat with a port number to run upon.
Tomcat tomcat = new Tomcat();</pre>
tomcat.setPort(Integer.valueOf(webPort));

The command above instantiated tomcat and sets the port to listen to as 8080.

Now we need to guide tomcat to deploy our application.  This is done as

tomcat.addWebapp("/", new File("src/main/webapp/").getAbsolutePath());

Once web application is added to tomcat, we can start it. It runs as a thread and awaits requests. The command is

tomcat.start();
tomcat.getServer().await();

And ta-da.

Now its time to compile and run our application.
First we clean maven directory, compile the code and package it as

mvn clean install

The packaging creates target directory where all the temporary artifacts are kept. The war file is kept here as “embeddedTomcatSample” because this what we mentioned in POM dependencies section. This JAR is run using the batch file generated in target\bin. Run it as

target\bin\webapp.bat

This would start the tomcat server for us.