Posts

Showing posts from 2012

CXF RESTFUL Service - Example jax-rs

CXF frame work  supports restful web services.
It is provided in the following ways:
JAX_RSJAX_WS ProviderDispatchHtppBinding Below is the example of RESTFUL service which gives an xml response.The data is pulled form  the database.I have also integrated  spring-jdbc with cxf.

check the response after running the application at-http://localhost:8086/cxf-jaxrs-poc/myservice/users
Port number can change based on availability.

Lets start with the configurations-cxf.xml:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd" default-lazy-init="false"> …

cxf-jaxws-SOAP Example-Live Resturant Example

This is a simple cxf-jaxws application.The appliction offers 2 services
cancel orderplace order The wsdl can be accessed at: http://localhost:8086/cxf-jaxws-LiveResturant/OrderService?wsdl
CXF webservice depends on the service endpoint interface.
Uses spring based configuration.
The application has been tested on tomcat and uses the latest cxf jar files.
Create a web application project in eclipsecxf.xml -add a cxf.xml in web-inf folder <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:wsa="http://cxf.apache.org/ws/addressing" xmlns:wsrm-mgr="http://cxf.apache.org/ws/rm/manager" xmlns:wsp="http://www.w3.org/2006/07/ws-policy" xmlns:wsrm="http://schemas.xmlsoap.org/ws/2005/02/rm/policy" xmlns:wsam="http://www.w3.org/2007/02/addressing/metadata" xmlns:mt…

Code First Webservice using jax-ws and CXF- webservice framework

Create a BeanCreate a SEI-Service End Point InterfaceUse Annotation on the SEI @ webserviceCreate a Implementation Class which implements the SEICreate a CXF.xml file add the  SEI and impl mapping in the xml fileconfigure the cxf file in the web.xmlmap the  spring context listener and cxf servlet in the web.xml.

What are the Front ends Provided by Apache CXF

JAX-WSJAX-RSSimpleJavaScript

What is Bindings in Webservice and types of Bindings

Bindings:Maps a webservice message to the protocol.

Types of Bindings:

SOAP bindings:Maps a webservice message to a SOAP service
Pure XML Bindings:sends a raw xml message
HTTP Bindings:Maps webservice message using a RESTFUL service.

Creating threadpool to execute tasks

The below code shows  usage of  util.concourrent.ExecutorService to create a thread pool.
The tasks are executed concurrently using the avilable number of fixed threads.
Below link tells us the difference of thread vs runnable interface


3) Runnable interface represent a Task which can be executed by either plain Thread or Executors or any other means. so logical separation of Task asRunnable than Thread is good design decision.
Read more: http://javarevisited.blogspot.com/2012/01/difference-thread-vs-runnable-interface.html#ixzz22rz8RZqi



package com.anjana.threadpools; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import com.anjana.dao.TenantManager; public class Main { private static final int NTHREDS = 4; public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(NTHREDS); TenantManager tm=new Tenan…

Creating a portlet project using maven archetype

mvn archetype:create -DgroupId=com.myportlet -DartifactId=myportletproject -DarchetypeArtifactId=maven-archetype-portlet

Integrating Struts2 and spring security -with apache ds

Pre-requisites: Apache directory studio-1.5.3Apache- Ds server 1.5Jboss5.1gaEclipseJava 1.6
Add  a application-security-ldap.xml to web-inf folder.

The application-secuirty-ldap.xml file  contains 2 parts  that needs to be configured: http and the authentication manager

The http tag-contains url that needs to be authenticated and  and authorised with role.
If a user is authenticated and not authorised -then the app redirects the user to access denied page.This configuration is done in web.xml file.


The second part is the <authentication-manager> this has information about type of authentication provider used, its urls and credentials.In the below example I have used ldap-server which connects with apache ds which is listening at port:10389 and the partition that needs to be searched is "sevenSeas".
The ldap-authentication provider gives input on what attributes needs to be searched for the user name.In the configuration- mail is mapped as username  and roles are retrieved fro…

Starting apache ds server

Control Panel->Administrative Tools->Services

Click on Apache Directory Server- in the left side you would get an option to start the server.
Click on start.

Creating partition in apache directory server and loading the context

Install-apache ds1.5 and apache directory studio.

Creating partition in apache directory server and loading the context

Open server.xml file--->C:\Program Files\Apache Directory Server\instances\default\conf\server.xml
add jdm partition-   
<jdbmPartition id="prdc" suffix="o=prdc"/> 
You can restart the apache ds server to see the changes.

Then connect to Apache DS using the Apache Directory Studio.
Right Click on DIT
Create Entry from Scratch  select Organization 
select o and in the value enter-"prdc"
select finish
and refesh the DIT to see the updates.

JBOSS-javax.inject.DefinitionException: import not a Java type

I got this error while executing spring-jdbc with spring secuirty.
The reason is the name- of the beans.xml file.
Looks like jboss doesnt like beans.xml .
I renamed my beans.xml file to jdbc-beans.xml.
The error was gone.

Integrating Struts2 with Spring Security using Custom Login Form

Integrating Struts2 with Spring Security involves few configurations.

Add all the spring core and spring security related jars-- spring -3.0.6

Struts2-Action :gets user principal and displays it on jsp after authentication
package com.prdc.spring3; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import java.util.Iterator; import java.util.List; import java.util.Properties; import java.util.Set; import org.apache.struts2.ServletActionContext; public class HelloWorld { private String message; private String username; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String execute() { /*to get the authenticated username*/ HttpServletRequest request = ServletActionContext.getRequest(); this.setUsername(request.getUserPrincipal().getName()); …

How Does DriverManger -return a Connection Object

DriverManger has a static method called -static Connection getConnection(String url, String user, String password)--> Attempts to establish a connection to the given database URL.

which returns a Connection Object.
The returned Connection Object is an Interface.
Connection con=DriverManger.getConnection(String url, String user, String password)


http://docs.oracle.com/javase/1.5.0/docs/api/java/sql/DriverManager.html

Update the LOG Level at Runtime without Restarting the server

Update the LOG Level at Runtime without Restarting the server- Thanks to avajava.com.I have enchnaced the code by providing a jsp file and adding log4j.xml properties as well.Find the update src code and screen shots in the PDF below. Log4j-Update Log Level of the Application without restarting the server.PDF

JDOM-Modify xml or add new elements to XML

The JDOM api is used for modifying the xml and adding new Element and values to the existing xml. /*
 * add jdom2.2 jaR TO CLASSPATH FOR THE PROGRAM TO WORK*/
jaxen, xercesImpl.jar,xml-apis.jar

Download the source code from the below link.
The Jar file contains a main program, which demonstates how to use the JDOM api.
All we have to do is modify the main program-with your file name
and provide the parameters.
Add the other source file to the class path.Or add the jar to the class path with above mentioned JAR-Jdom and xml related jar

https://docs.google.com/open?id=0Bw0JiVGLs_kUckZmSWt4NGpFNms

package com.anjana.xmlutils;

import java.io.File;

public class ModifyMain {
    public static void main(String args[]){
        File xmlFile = new File("c:\\myfile.xml");
        ModifyXMLFileClient modifyclient=new ModifyXMLFileClient(xmlFile);
        modifyclient.modify_xmlattribute("staff","id","3");
        modifyclient.add_xmlelement("staff","…

JAAS Authentication Process

JAAS Authentication Process:

Creating  an Instance of Login context-using the constructor- LoginContext(String,CallbackHandler)The First paramenter takes the name of the configuration file Second Parameter is a callbackhander it has a Handle() method-this passes the information to the loginModule.An empty Subject is created before authentication begins-this subject is passed to the login modules.On Success the SUbject is attached with credentials.login() method is called to instiate the  login process-after success the application can retrive the Subject using getSubject();on failure throws authentication failure exception.