Christmas Seasonal Articles .... :)
Refer below WSO2 Articles ....
[1] http://wso2.com/library/articles/2014/06/how-to-write-connector-integration-tests-using-wso2-test-automation-framework/
[2] http://wso2.com/library/articles/2014/10/article-introducing-ui-tests-for-wso2-products/
Friday, December 12, 2014
Monday, December 8, 2014
Sample Jaxrs service .....
Here is a sample Jaxrs service which you can use as a backend hosted in Tomcat server.
Package structure ...
* musicsample
|- bean
|- Music
|- JaxRsApiApplication
|- MusicConfig
|- MusicRestService
|- MusicService
|- Server
Server.java
package org.wso2.automation.musicsample;
import org.apache.catalina.Context;
import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.startup.Tomcat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import java.io.File;
import java.io.IOException;
public class Server {
private final static Log log = LogFactory.getLog(Server.class);
public static void main(final String[] args) throws Exception {
final File base = createBaseDirectory();
log.info("Using base folder: " + base.getAbsolutePath());
final Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
tomcat.setBaseDir( base.getAbsolutePath() );
Context context = tomcat.addContext( "/", base.getAbsolutePath() );
Tomcat.addServlet( context, "CXFServlet", new CXFServlet() );
context.addServletMapping( "/rest/*", "CXFServlet" );
context.addApplicationListener( ContextLoaderListener.class.getName() );
context.setLoader( new WebappLoader( Thread.currentThread().getContextClassLoader() ) );
context.addParameter( "contextClass", AnnotationConfigWebApplicationContext.class.getName() );
context.addParameter( "contextConfigLocation", MusicConfig.class.getName() );
tomcat.start();
tomcat.getServer().await();
}
private static File createBaseDirectory() throws IOException {
final File base = File.createTempFile( "tmp-", "", new File("/home/dimuthu/Desktop/JMS"));
if( !base.delete() ) {
throw new IOException( "Cannot (re)create base folder: " + base.getAbsolutePath() );
}
if( !base.mkdir() ) {
throw new IOException( "Cannot create base folder: " + base.getAbsolutePath() );
}
return base;
}
}
MusicService.java
package org.wso2.automation.musicsample;
import org.springframework.stereotype.Service;
import org.wso2.automation.musicsample.bean.Music;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class MusicService {
private final ConcurrentHashMap< String, Music> musicCollection = new ConcurrentHashMap< String, Music >();
public MusicService() {
init();
}
final void init() {
System.out.println("Welcome To World Of Music .... ");
Music music = new Music();
music.setAlbum("Gold");
music.setSinger("Elton John");
musicCollection.put(music.getAlbum(), music);
}
public Music getByAlbum( final String albumName ) {
return musicCollection.get(albumName);
}
public void setMusic( Music music ) {
musicCollection.put(music.getAlbum(),music);
}
}
MusicRestService.java
package org.wso2.automation.musicsample;
import org.wso2.automation.musicsample.bean.Music;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/music")
public class MusicRestService {
@Inject
private MusicService musicService;
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Music getMusicInJSON(@QueryParam("album") final String albumName) {
/* Music music = new Music();
music.setAlbum("Beat It !!!");
music.setSinger("Micheal Jackson");*/
return musicService.getByAlbum(albumName);
//return musicService.musicCollection.get("Dimuthu");
}
@POST
@Path("/post")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createMusicInJSONPOST(Music music) {
musicService.setMusic(music);
String result = "Album Added : " + music;
return Response.status(201).entity(result).build();
//return music.getAlbum();
}
@PUT
@Path("/put")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createMusicInJSONPUT(Music music) {
musicService.setMusic(music);
String result = "Album Added : " + music;
return Response.status(200).entity(result).build();
//return music;
}
}
MusicConfig.java
package org.wso2.automation.musicsample;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.ws.rs.ext.RuntimeDelegate;
import java.util.Arrays;
@Configuration
public class MusicConfig {
@Bean( destroyMethod = "shutdown" )
public SpringBus cxf() {
return new SpringBus();
}
@Bean
public Server jaxRsServer() {
JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint( jaxRsApiApplication(), JAXRSServerFactoryBean.class );
factory.setServiceBeans( Arrays.< Object >asList(musicRestService()) );
factory.setAddress( "/" + factory.getAddress() );
factory.setProviders( Arrays.< Object >asList( jsonProvider() ) );
return factory.create();
}
@Bean
public org.wso2.automation.musicsample.JaxRsApiApplication jaxRsApiApplication() {
return new JaxRsApiApplication();
}
@Bean
public MusicRestService musicRestService() {
return new MusicRestService();
}
@Bean
public MusicService musicService() {
return new MusicService();
}
@Bean
public JacksonJsonProvider jsonProvider() {
return new JacksonJsonProvider();
}
}
Music.java
package org.wso2.automation.musicsample.bean;
public class Music {
String album;
String singer;
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
@Override
public String toString() {
return "Your Album " + album + " Of " + singer + " Was Added Successfully";
}
}
JaxRsApiApplication.java
package org.wso2.automation.musicsample;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath( "api" )
public class JaxRsApiApplication extends Application {
}
Here is a sample Jaxrs service which you can use as a backend hosted in Tomcat server.
Package structure ...
* musicsample
|- bean
|- Music
|- JaxRsApiApplication
|- MusicConfig
|- MusicRestService
|- MusicService
|- Server
Server.java
package org.wso2.automation.musicsample;
import org.apache.catalina.Context;
import org.apache.catalina.loader.WebappLoader;
import org.apache.catalina.startup.Tomcat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import java.io.File;
import java.io.IOException;
public class Server {
private final static Log log = LogFactory.getLog(Server.class);
public static void main(final String[] args) throws Exception {
final File base = createBaseDirectory();
log.info("Using base folder: " + base.getAbsolutePath());
final Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
tomcat.setBaseDir( base.getAbsolutePath() );
Context context = tomcat.addContext( "/", base.getAbsolutePath() );
Tomcat.addServlet( context, "CXFServlet", new CXFServlet() );
context.addServletMapping( "/rest/*", "CXFServlet" );
context.addApplicationListener( ContextLoaderListener.class.getName() );
context.setLoader( new WebappLoader( Thread.currentThread().getContextClassLoader() ) );
context.addParameter( "contextClass", AnnotationConfigWebApplicationContext.class.getName() );
context.addParameter( "contextConfigLocation", MusicConfig.class.getName() );
tomcat.start();
tomcat.getServer().await();
}
private static File createBaseDirectory() throws IOException {
final File base = File.createTempFile( "tmp-", "", new File("/home/dimuthu/Desktop/JMS"));
if( !base.delete() ) {
throw new IOException( "Cannot (re)create base folder: " + base.getAbsolutePath() );
}
if( !base.mkdir() ) {
throw new IOException( "Cannot create base folder: " + base.getAbsolutePath() );
}
return base;
}
}
MusicService.java
package org.wso2.automation.musicsample;
import org.springframework.stereotype.Service;
import org.wso2.automation.musicsample.bean.Music;
import java.util.concurrent.ConcurrentHashMap;
@Service
public class MusicService {
private final ConcurrentHashMap< String, Music> musicCollection = new ConcurrentHashMap< String, Music >();
public MusicService() {
init();
}
final void init() {
System.out.println("Welcome To World Of Music .... ");
Music music = new Music();
music.setAlbum("Gold");
music.setSinger("Elton John");
musicCollection.put(music.getAlbum(), music);
}
public Music getByAlbum( final String albumName ) {
return musicCollection.get(albumName);
}
public void setMusic( Music music ) {
musicCollection.put(music.getAlbum(),music);
}
}
MusicRestService.java
package org.wso2.automation.musicsample;
import org.wso2.automation.musicsample.bean.Music;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/music")
public class MusicRestService {
@Inject
private MusicService musicService;
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_JSON)
public Music getMusicInJSON(@QueryParam("album") final String albumName) {
/* Music music = new Music();
music.setAlbum("Beat It !!!");
music.setSinger("Micheal Jackson");*/
return musicService.getByAlbum(albumName);
//return musicService.musicCollection.get("Dimuthu");
}
@POST
@Path("/post")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createMusicInJSONPOST(Music music) {
musicService.setMusic(music);
String result = "Album Added : " + music;
return Response.status(201).entity(result).build();
//return music.getAlbum();
}
@PUT
@Path("/put")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createMusicInJSONPUT(Music music) {
musicService.setMusic(music);
String result = "Album Added : " + music;
return Response.status(200).entity(result).build();
//return music;
}
}
MusicConfig.java
package org.wso2.automation.musicsample;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.ws.rs.ext.RuntimeDelegate;
import java.util.Arrays;
@Configuration
public class MusicConfig {
@Bean( destroyMethod = "shutdown" )
public SpringBus cxf() {
return new SpringBus();
}
@Bean
public Server jaxRsServer() {
JAXRSServerFactoryBean factory = RuntimeDelegate.getInstance().createEndpoint( jaxRsApiApplication(), JAXRSServerFactoryBean.class );
factory.setServiceBeans( Arrays.< Object >asList(musicRestService()) );
factory.setAddress( "/" + factory.getAddress() );
factory.setProviders( Arrays.< Object >asList( jsonProvider() ) );
return factory.create();
}
@Bean
public org.wso2.automation.musicsample.JaxRsApiApplication jaxRsApiApplication() {
return new JaxRsApiApplication();
}
@Bean
public MusicRestService musicRestService() {
return new MusicRestService();
}
@Bean
public MusicService musicService() {
return new MusicService();
}
@Bean
public JacksonJsonProvider jsonProvider() {
return new JacksonJsonProvider();
}
}
Music.java
package org.wso2.automation.musicsample.bean;
public class Music {
String album;
String singer;
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
@Override
public String toString() {
return "Your Album " + album + " Of " + singer + " Was Added Successfully";
}
}
JaxRsApiApplication.java
package org.wso2.automation.musicsample;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath( "api" )
public class JaxRsApiApplication extends Application {
}
Subscribe to:
Posts (Atom)