SOA / Web Services / Java

A Technology Blog

SOA Services/ Web Services Vs APIs

Posted by Vivek on May 8, 2016

With a lot of emphasis of building APIs, web services have taken a back seat but it certainly does not mean that APIs can completely replace the conventional web services within an SOA environment. However, with the rapid technological advances and changing business requirements, most of the implementations have become API-centric. This post just highlights some of the key differences and draw a line between the two.

1. Mostly used internally by organizations or partners whereas APIs are mostly built for external consumers. APIs are seen as a way to unlock new revenue streams by creating a marketplace.

2. SOA services are mainly need based and are driven by existing IT or Business demands. APIs are developed keeping the future needs in mind and are key enabler for mobile and web based applications. SOA or webservices are suitable candidates in an event driven architecture.

3. SOA services are standard based and all standards have reached a certain level of maturity and are adopted by industries. APIs are often not standard based. There is no mandate to create a contract unlike that of WSDL in SOA.

4. Security is a key challenge when using APIs as most of them are exposed to the outside world. Most of the SOA Services are behind firewall and are within DMZs.

5. Scaling is another aspect to look at when using APIs. With an increase in the number of consumers, appropriate controls have to be in place for scaling in and scaling out APIs. For SOA services, it is easier to predict the volume of requests as they are driven by the internal business needs. A private API can be considered closer to SOA services.

6. APIs are mostly finely-grained to make them more reusable. Granurality in SOA Services can be coarse and is designed to make them reusable as well as composable.

7. API protyping tools like Apiary, Swagger etc help in rapidly developing interfaces and are easy to use. This is not always the case when you are designing services for SOA.

8. Services in SOA are well-equipped to handle complex integration challenges posed by different message formats, transport protocols and semantic standards. Almost all APIs are designed to communicate only using HTTP(S) protocols but the scenario is changing fast with the proliferation of Intenet of Things and the need for more asynchronous communication.

 

 

 

Posted in API, SOA | Leave a Comment »

Building REST Service made easy using Eclipse Plugin

Posted by Vivek on May 7, 2016

RESTful services have become increasingly popular and as a result there are various frameworks that supports the development of REST Services. Each has its own advantages but some of the widely used are

  • Jersey
  • RestEasy
  • Restlet
  • Spring

Eclipse provides a plugin that leverages these frameworks and helps in rapidly developing services.

If you do not have eclipse installed on your machine, then download the installer from https://eclipse.org/downloads/

In order to start with developing REST Services, please follow the below URL to download the plugin

https://marketplace.eclipse.org/content/restful-plugin-eclipse

Once the plugin is installed successfully, you should be able to see a new category “Restful Webservice” in the wizard menu

This plugin also provides capability to add jars or libraries to the project and generate template classes.

Now create a new project in eclipse. Choose the dymanic web project as an option to create the project. Name the project and click on Next. Leave the other options as default ones.In the Web Module screen, check the “Generate web.xml deployment descriptor” option and click Finish.

To make the project Jersey compliant, right click on project root and select New->other.

Go to the Restful Webservice option and choose the Jersey RESTful Webservice. It will launch the Jersey Restful Webservice wizard.

Enter the package name and click on Finish. Do not modify any other values.

Web.xml file gets updated and jersey jars are added to the lib folder.

A java class (with the same name as that in wizard) gets generated with Jersey supported annotations. The class is your starting point to develop a  REST service. Here is how it will look like:

@Path(“/<add your restful service class name here>”)

public class SimpleRestService {

private static final Logger logger = Logger.getLogger(SimpleRestService.class);

The @Path annotation will define the service class name. Similarly, every resource in the service has to be identified uniquely using the @Path annotation at method level.

Similarly, you can make the project RestEasy compliant. In this case, class generated will look like as follows:

@Path(“/<add your restful service class name here>”)

public class SimpleRestService {

private static final Logger logger = Logger.getLogger(SimpleRestService.class);

@GET

@Path(“/<add method name here>”)

@Produces(MediaType.TEXT_PLAIN)

public String getSomething(@QueryParam(“request”) String request ,

@DefaultValue(“1”) @QueryParam(“version”) int version) {

……………………..

 

A Restlet compliant class will look like as follows:

public class SimpleRestService extends ServerResource {

private static final Logger logger = Logger.getLogger(SimpleRestService.class);

@Get

public String getSomething() {

…………………..

 

A Spring compliant class will be seen like the one below:

@RestController

@RequestMapping(“/<add your restful service class name here>”)

public class SimpleRestController {

// Logger instance

private static final Logger logger = Logger.getLogger(SimpleRestController.class);

@RequestMapping(value = “/<add method name here>”, method = RequestMethod.GET)

public String getSomething(@RequestParam(value = “request”) String request, @RequestParam(value = “version”, required = false, defaultValue = “1”) int version) {

if (logger.isDebugEnabled()) {

logger.debug(“Start getSomething”);

logger.debug(“data: ‘” + request + “‘”);

}

 

 

 

 

Posted in SOA | Leave a Comment »

REST Client using Apache Wink

Posted by Vivek on May 7, 2016

This post shows how to write a client in java using apache wink framework to invoke a REST based service. Apache Wink is a powerful framework to build RESTful webservices and has both client and server modules. We will use the client module which is built on top of the JDK HttpURLConnection.

  1. Getting the dependencies: You need to get all the jars or libraries in place. You can use maven to download all the jars. These are jars that we will keep handy. Please note that not all jars are required but for different REST service and testing requirements, you will need them sooner or later.

activation-1.1.jar
analytics-core-5.2.1.jar
animal-sniffer-annotations-1.11.jar
bcprov-jdk15-144.jar
binding-2.0.1.jar
cajo-1.142.jar
commons-beanutils-1.7.0.jar
commons-cli-1.2.jar
commons-codec-1.3.jar
commons-collections-3.2.1.jar
commons-httpclient-3.1.jar
commons-io-2.4.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
cssparser-0.9.5.jar
dom4j-1.6.1.jar
ezmorph-1.0.5.jar
flex-messaging-common-1.0.jar
flex-messaging-core-1.0.jar
flex-messaging-opt-1.0.jar
flex-messaging-proxy-1.0.jar
flex-messaging-remoting-1.0.jar
forms-1.0.7.jar
groovy-all-2.1.7.jar
guava-14.0.jar
hamcrest-core-1.3.jar
hermes-1.14.jar
htmlunit-2.7.jar
htmlunit-core-js-2.7.jar
httpclient-4.1.1.jar
httpclient-cache-4.1.1.jar
httpcore-4.1.1.jar
httpcore-nio-4.1.1.jar
httpmime-4.1.1.jar
i4jruntime-5.1.14.jar
javassist-3.16.1-GA.jar
jaxen-1.1-beta-8.jar
jcifs-1.2.9.jar
jdom-1.0.jar
jettison-1.2.jar
jetty-6.1.26.jar
jetty-util-6.1.26.jar
jms-1.1.jar
joda-time-1.6.2.jar
js-1.7R2.jar
json-lib-2.2.2-jdk15.jar
json-path-0.9.1.jar
json-smart-1.2.jar
jsr173_api-1.0.jar
jtidy-r872-jdk15.jar
junit-4.11.jar
log4j-1.2.14.jar
looks-2.2.0.jar
mail-1.4.jar
maven-plugin-api-2.0.jar
nekohtml-1.9.14.jar
not-yet-commons-ssl-0.3.11.jar
opensaml-2.5.1-1.jar
openws-1.4.2-1.jar
org.apache.oltu.oauth2.client-0.31.jar
org.apache.oltu.oauth2.common-0.31.jar
org.apache.oltu.oauth2.httpclient4-0.31.jar
out-app-analytics-provider-5.2.1.jar
proxy-vole-20131209.jar
reflections-0.9.9-RC1.jar
rsyntaxtextarea-2.5.0.jar
sac-1.3.jar
saxon-9.1.0.8j.jar
saxon-dom-9.1.0.8j.jar
serializer-2.7.1.jar
slf4j-api-1.6.1.jar
slf4j-simple-1.6.1.jar
soapui-5.2.1.jar
swingx-soapui.jar
wink-client-1.4.jar
            wink-common-1.4.jar
ws-commons-util-1.0.2.jar
wsdl4j-1.6.2-fixed.jar
wss4j-1.6.16.jar
xalan-2.7.1.jar
xbean_xpath-2.4.0.jar
xbean-fixed-2.4.0.jar
xercesImpl-2.9.1.jar
xml-apis-1.3.04.jar
xml-apis-2.9.1.jar
xmlParserAPIs-2.6.2.jar
xmlpublic-2.4.0.jar
xmlsec-1.4.5.jar
xmltooling-1.3.2-1.jar
xmlunit-1.2.jar
xom-1.1.jar
xstream-1.3.1.jar

2. Create the client configuration object

org.apache.wink.client.ClientConfig clientConfig = new org.apache.wink.client.ClientConfig();

3. Create the client object to store the configuration:

org.apache.wink.client.RestClient client = new org.apache.wink.client.RestClient(clientConfig);

4. Create the resource object setting the Service URL. Replace the <rest-url> with the actual URL to invoke the service.

org.apache.wink.client.Resource resource = client.resource(<rest-url>);

5. Invoke the service.

To call a GET service, use the following line:

org.apache.wink.client.ClientResponse resp = resource.get();

For PUT,

org.apache.wink.client.ClientResponse resp2 = resource.contentType(<media-type>).put(“<message>”);

Similarly for POST,

resource.contentType(<content-type>).accept(<media-type>).post(<message>);

6. Get the response message in String format, using

resp.getEntity(String.class)

Get the HTTP Status Code to determine if the request was successfully processed.

resp.getStatusCode()

For successful invocation, the service should return 2xx status code. For complete list of HTTP codes, please refer https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

 

Posted in SOA | Leave a Comment »

Interested in Agile Certification?

Posted by Vivek on May 7, 2016

Here are a few good ones:

Certification Name / Certification Agency Link Price
PMI Agile Certified Practitioner (PMI ACP) Project Management Institute http://www.pmi.org/Certification/New-PMI-Agile-Certification.aspx

 

$500 (exam only)
Certified Scrum Product Owner (CSPO) Scrum Alliance https://www.scrumalliance.org/certifications/practitioners/cspo-certification 1400€ for 2 day course
Certified Scrum Master (CSM) Scrum Alliance http://www.scrumalliance.org/certifications/practitioners/certified-scrummaster-%28csm%29

 

1400€ for 2 day course
Certified Scrum Developer (CSD) Scrum Alliance https://www.scrumalliance.org/certifications/practitioners/csd-certification $2500 for 3 day course + CSM cost
Certified Scrum Professional (CSP) Scrum Alliance https://www.scrumalliance.org/certifications/practitioners/csp-certification $250
Professional Scrum Master I (PSM) Scrum.org https://www.scrum.org/Assessments/Professional-Scrum-Master-Assessments/PSM-I-Assessment

 

100$ (exam only)
Scrum Master Accredited Certification Scrum Institute http://www.scrum-institute.org/Scrum_Master_Accredited_Certification_Program.php 29$ (exam only)
Safe Practicioner (SP) Scaled Agile Framework / Scaled Agile Academy https://scaledagileacademy.site-ym.com/?page=safepractitioner

 

$1400 for 2 day course
Certified Agile Essentials ISQI https://agileessentials.org/

 

1-2 day

course

Scrum Master Foundation Scrum Association http://www.scrum.as/ $29 exam only

Posted in SOA | Leave a Comment »

10 REST Best Practices

Posted by Vivek on May 7, 2016

1. Use uniform contracts to allow consumers to comply with the service requirements, make the service discoverable, loosely coupled and composable.

2. Use POST when you want to create a sub resource. Use POST when you want to update the state of a resource. PUT ensures that a resource or resource value remains unique while duplicate instances are possible when using POST.

3. Use DELETE when you no longer require the resource.

4. When using GET, consider caching the static data in the response. Request should not contain more than 4KB data as it may lead to “414 – Request_URI Too Long” error.

5. Endpoint URI may undergo changes. Use the native HTTP endpoint redirection capability to redirect a request to new URI. HTTp provides status codes such as 301 for “Moved Permanently” and 307 for “Temporary Redirect”

6. Choose the right granularity. A coarse grained service is not always reusable. At the same time, a finer grained service may not be good from performance perspective.

7. When defining URIs,
– use nouns
– use tokens (eg bearer) for authorization/ authentications
– keep the URIs short
– avoid using content type to retain abstraction
– use positional parameters in the query string.

8. A service should be backward compatible.

9. Standard Media types should be clearly specified in the contract. Client should provide valid formats in the “Accept”, “Accept-Language”, “Accept-Charset”, “Accept-Encoding” and “Content-Type” to avoid any interoperability issues. Let the server tell the client if a format is not supported using valid HTTP Code.

10. Use reliable messaging techniques like MQ for critical operations when a message lost due to network failure is unacceptable and message should be retried. While the retry mechanism can be implemented using REST, it should not become complex.

Posted in API, REST, SOA | Leave a Comment »

How to access Amazon AWS S3?

Posted by Vivek on August 22, 2015

Here are the steps to access Amazon S3 to access files stored on cloud.

  1. Download AWS CLI Installer from https://s3.amazonaws.com/aws-cli/AWSCLI64.msi. It can be 32 bit or 64 bit depending your system’s setting.
  2. Once downloaded, Install the AWS CLI
  3. Open the Command Prompt and navigate to the directory where AWS client is installed.
  4. Enter the command from the bin directory- aws configure

Provide the Access Key and Secret Access Key when asked. You can leave the region name and output format fields blank. Example below:

C:\windows\system32>aws configure

AWS Access Key ID [None]: [Access Key]

AWS Secret Access Key [None]: [Secret Access Key]

Default region name [None]:

Default output format [None]:

You can use clients such as S3 Browser to connect and access the files.

If you want to change the credentials, go to user home. You should be able to find .aws directory. It has two files – config and credentials. Credentials file should have access key id and secret access key.

Posted in SOA | Leave a Comment »

Testing/ Mocking WebServices using browser based plugins

Posted by Vivek on June 11, 2015

REST has rapidly become a part of almost all SOA or integration architectures. While there are tools like SOAPUi that can be used to test REST services, there are plugins which are more light-weight and provide good features

1. SOA Client (Firefox plugin)

It can be used to access SOAP based web services as well as UDDI registries. It can be added using the following link:

2. Postman (Chrome plugin)

It is an advanced REST service testing tool.

3. Mockable.io

It is not a plugin but a site that allows user to mock services and provides them with a unique URL which can be shared with other users.

Not just REST, it also allows to mock SOAP services.

Posted in SOA | Leave a Comment »

Ruby: Working with Postgres Database

Posted by Vivek on June 10, 2015

Firstly, install a ruby gem pg using the following command

gem install  pg

Then write a class to connect to database and retrieve values from a table

class PostGresClient

#!/usr/bin/env ruby

require ‘pg’

# Output a table of current connections to the DB
conn = PG.connect(“localhost”, 5432, , , “”, “”, “”)
res = conn.exec( “SELECT * FROM public.\”Employee\”)
res.each do |row|
row.each do |column|
puts column
end
end

end

Posted in SOA | Leave a Comment »

Ruby: Writing message to ActiveMQ

Posted by Vivek on June 10, 2015

The following piece of code can be used to write a message to ActiveMQ queue

require ‘rubygems’
require ‘jms’

config = {
:factory => ‘org.apache.activemq.ActiveMQConnectionFactory’,
:broker_url => ‘tcp://localhost:61616’,
:require_jars => [“~/Applications/apache-activemq-5.4.2/activemq-all-5.4.2.jar”]
}

JMS::Connection.session(config) do |session|
session.producer(:q_name => ‘myQueue’) do |producer|
producer.send(session.message(“Helloworld”))
end
end

Posted in SOA | Leave a Comment »

Ruby: Reading message from ActiveMQ

Posted by Vivek on June 10, 2015

The following piece of code can be used to read a message from queue using Ruby.

# Include JMS after ActiveMQ
require ‘rubygems’

require ‘jms’


# Connect to ActiveMQ
config = {
:factory => ‘org.apache.activemq.ActiveMQConnectionFactory’,
:broker_url => ‘tcp://localhost:61616’,
:require_jars => [“~/Applications/apache-activemq-5.4.2/activemq-all-5.4.2.jar”]
}
JMS::Connection.session(config) do |session|
session.consume(:q_name => ‘myQueue’, :timeout=>1000) do |message|
p message
end
end

Posted in SOA | Leave a Comment »