Tuesday, December 20, 2016

Java SOAP WebService and WebService Client with Apache Axis



Creating and deploying a simple WebService is not as hard as you think. For this example we are going to work with Apache Axis WebService. Axis is essentially a SOAP engine ,a framework for constructing SOAP processors such as clients, servers, gateways, etc.
Apache Axis is a SOAP toolkit that makes it easy to create, deploy, and consume web services. By using Axis, you will be able to quickly convert existing Java functionality into web services, deploy those services, and be able to communicate with them remotely via the Internet, usually through firewalls (if necessary).

More precisely, Axis is an implementation of SOAP. Most people like to think of it as a toolkit. From the programmer's point of view, it's like an API. It makes it easy for you to communicate - with SOAP - to a remote object, without having to worry about the details associated with the protocol. In practice, it is a lot like using RPC, CORBA, RMI, or any of the other distributed computing technologies you may have experience with. The process is largely the same: you describe the target of your communication, invoke the remote method and marshall your parameters, and then demarshall the return values. Since AXIS makes this process simpler than ever, development is a breeze.

Well, let's start coding...
For this example we are going to use:
  • Java (here I'm using JDK 1.7, Click here if you need help installing Java JDK in Windows)
  • Apache Tomcat v7.xx
  • Eclipse Luna

Install Apache Tomcat and add it to Eclipse... File --> New --> Other. Type in server, and follow the wizard.

Open Eclipse and create a "Dynamic Web Project"
For project Name: JavaApacheAxisWebService




On your Eclipse project, expand Java Resources and under /src create a 
new class: MyJavaWebServiceHelloWorld 
and for Package: rolandofebrero.com.web.service



                                 


Now, double click on MyJavaWebServiceHelloWorld.java and add the following code:



Right click on MyJavaWebServiceHelloWorld.java --> Web Services --> Create Web Service


                                   



On the next screen, make sure you have selected Tomcat and Apache Axis in both service and client. Please see the image bellow:



If after clicking finish you see any error regarding to you server, make sure you can start Tomcat with no errors. If not, delete your server, and add it again. Server should start with no errors before creating WebService.

If everything was OK, your project should look like this:



If the server started correctly, JavaApacheAxisWebService and JavaApacheAxisWebServiceClient are automatically deployed to Tomcat server.

By default Eclipse provides a Web service Test Client Window in which we can consume our service



If we click on getEndpoint() link, it will provide us with the endpoint of our service, which is:

        http://localhost:14823/JavaApacheAxisWebService/services/MyJavaWebServiceHelloWorld

put that endpoint in your browser and add ?WSDL at the end. We will see the WSDL created. If you want to consume this service, you might need the WSDL, or just use SoapUI and create a soap project and start testing this new service....    
     


You can download the code for this project from my GitHub account:

https://github.com/rolando-febrero/JavaApacheAxisWebService




Programming thought of the day:


  • My software never has bugs. It just develops random features.

Monday, December 5, 2016

Installing and Running Node.js applications on IIS – Windows



Go to: https://nodejs.org/en/#download and download Node.js for Windows. For this exercise, we are using v6.9.1 LTS version.
Place it in C:\node 



 Also, we need to install iisnode. Go to: https://github.com/tjanczuk/iisnode/releases

Download and install the version Windows OS requires (32 or 64 bits). For this exercise, we are using iisnode-full-v0.2.21-x64.msi





Install URL rewrite module for IIS. Go to: http://www.iis.net/downloads/microsoft/url-rewrite
URL Rewrite allows Web administrators to easily build powerful rules using rewrite providers written in .NET, regular expression pattern matching, and wildcard mapping to examine information in both URLs and other HTTP headers and IIS server variables. Rules can be written to generate URLs that can be easier for users to remember, simple for search engines to index, and allow URLs to follow a consistent and canonical host name format. URL Rewrite further simplifies the rule creation process with support for content rewriting, rule templates, rewrite maps, rule validation, and import of existing mod_rewrite rules.

For testing purposes, create a Default site in IIS to test our Node.js installation. Follow the next steps:

Go to Internet Information Services Manager (IIS). Right click on Sites and click on Add Web Site…


   


Add Web Site dialog box will prompt for some input. Fill out this dialog with:
·        Site Name: Default Web Site
·        Application pool: choose DefaultAppPool
·        Physical path: C:\Program Files\iisnode\www

Make sure to Test your settings before clicking OK.

  


To set up samples, from the administrative command prompt call:

%programfiles%\iisnode\setupsamples.bat   (it is recommended to run it as Administrator)

Or go to: C:\Program Files\iisnode and run setupsamples.bat from there

The setupsamples.bat will:
·        Unregister existing "iisnode" global module from your installation of IIS if such registration exists
·        Register iisnode as a native module with your installation of IIS
·        Install configuration schema for the "iisnode" module
·        Remove existing "iisnode" section from system.webServer section group in applicationHost.config
·        Add the "iisnode" section within the system.webServer section group in applicationHost.config
·        Delete the iisnode web application if it exists
·        Add a new site iisnode to IIS



At this point your site should look like:



And if you click on Modules for this Site in IIS7 you should see iisnode as a native module:


                      

If everything went well, you should be able to hit:  http://localhost/node




Navigate to: http://localhost/node/helloworld/hello.js and if you are able to see this:





It is because the installation was successful and your Node.js app is running within IIS on Windows!








Programming thought of the day:

  • Michael Sinz: “Programming is like sex, one mistake and you have to support it for the rest of your life.”

Node.js & Express - Hello world example



Node.js is an open-source, cross-platform JavaScript runtime environment for developing a diverse variety of tools and applications. Although Node.js is not a JavaScript framework, many of its basic modules are written in JavaScript, and developers can write new modules in JavaScript. The runtime environment interprets JavaScript using Google's V8 JavaScript engine.

I use Node.js at work, and find it to be very powerful. Forced to choose one word to describe Node.js, I'd say "interesting" (which is not a purely positive adjective). The community is vibrant and growing. JavaScript, despite its oddities can be a great language to code in. And you will daily rethink your own understanding of "best practice" and the patterns of well-structured code. There's an enormous energy of ideas flowing into Node.js right now, and working in it exposes you to all this thinking - great mental weightlifting.


Pros / Cons:
  • Pro: For a server guy, writing JavaScript on the backend has been a "gateway drug" to learning modern UI patterns. I no longer dread writing client code.
  • Pro: Tends to encourage proper error checking (err is returned by virtually all callbacks, nagging the programmer to handle it; also, async.js and other libraries handle the "fail if any of these subtasks fails" paradigm much better than typical synchronous code)
  • Pro: Some interesting and normally hard tasks become trivial - like getting status on tasks in flight, communicating between workers, or sharing cache state
  • Pro: Huge community and tons of great libraries based on a solid package manager (npm)
  • Con: JavaScript has no standard library. You get so used to importing functionality that it feels weird when you use JSON.parse or some other build in method that doesn't require adding an npm module. This means that there are five versions of everything. Even the modules included in the Node.js "core" have five more variants should you be unhappy with the default implementation. This leads to rapid evolution, but also some level of confusion.

Now, let's install Node.js and see how it working. Go to https://nodejs.org/en/ and download the version you need (in this case we are using v6.9.1 LTS)




Create a folder and let's name it: helloNode
Launch the Command Prompt and navigate to helloNode folder. Run: npm init
This will prompt you for some information about your node app and create package.json file....see the image bellow:





So you are in \helloNode folder, in Command Promt type: npm install express --save  
This command will add Express as dependency for our project. If you don't know what Express is, take a look here: Express .

After adding Express dependecies, a new folder (node_modules) is added to \helloNode. Now create a new file in \helloNode and let's name it index.js

In index.js add the following code:

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

 The app starts a server and listens on port 3000 for connections. The app responds with “Hello World!” for requests to the root URL (/) or route. For every other path, it will respond with a 404 Not Found.

At this point, your \helloNode should look like this:




Now, it's time to see our lil monster works! In Command Prompt type: node index.js
Go to: http://localhost:3000/
If you are able to see the "Hello World" message is because everything went as planned. If not, you might be missing something.   

You can see and download the code for this project from my git account:
https://github.com/rolando-febrero/helloNode





Programming thought of the day:


  • Hey! It compiles! Ship it!