The article that I wrote on the subject can be accessed in the following location.
http://wso2.com/library/tutorials/2014/11/tutorial-connecting-and-publishing-stats-and-notification-to-wso2-bam-from-wso2-greg/
Friday, November 14, 2014
Tuesday, October 28, 2014
Creating Roles and Assigning Permissions in WSO2 IS
In this article I will try to explain the ways to create and assign roles in WSO2 Identity Server. I'll walk you through on creating and assigning roles in the Management console UI and then outline the ways to do the same using an API and using a Java client programmatically.
Let's start the WSO2 Identity Server (IS) first. To download and run the WSO2 IS, look here.
Once started login to management console. First let's look at how we can create a role named 'TestRole' and assign some permissions in the permission tree to it. This involves only a very few steps. Navigate to Configure -> Users and Roles -> Roles -> Add New Role. Enter the role name as 'TestRole' and click next. The permission tree would be shown and you need to select the the relevant permission in the tree for this role. If you need to assign certain existing users to this role, click next and select those users. Otherwise click finish. That's it. You have successfully create a a new role named 'TestRole' and assigned permission for that. You can see the existing roles by navigating to Home -> Configure -> Users and Roles -> Roles. At this place you can view the permission, delete the role, assign user and also rename the role.
Next we shall see how to create roles and assign any permission in the permission tree to those roles using a programmatic way, without using the UI. You can do that by calling an admin service as an API or using a Java client. I'll outline both the methods here.
There is a web service API called RemoteUserStoreManagerService that can be used to manage users and roles. This is an Admin Service in the WSO2 carbon platform. Admin services in the WSO2 products are hidden by default. To see the wsdl of this web service you need to unhide the Admin service WSDLs. To do that, first, open up CARBON_HOME/repository/conf/carbon.xml and look for the following line.
After the server is successfully started, you can access the wsdl of the RemoteUserStoreManagerService by navigating to https://localhost:9443/services/RemoteUserStoreManagerService?wsdl (Replace 'localhost' part as applicable).
Following are the two methods I mentioned
1. You can create a SOAP UI project with this wsdl. You can use the addRole method to add the role. A sample SOAP call is given below to add a role named 'ValidRole' and assign permission under '/permission/admin/login/EmailLogin' where 'EmailLogin' being a new permission I created under '/permission/admin/login/'
Let's start the WSO2 Identity Server (IS) first. To download and run the WSO2 IS, look here.
Once started login to management console. First let's look at how we can create a role named 'TestRole' and assign some permissions in the permission tree to it. This involves only a very few steps. Navigate to Configure -> Users and Roles -> Roles -> Add New Role. Enter the role name as 'TestRole' and click next. The permission tree would be shown and you need to select the the relevant permission in the tree for this role. If you need to assign certain existing users to this role, click next and select those users. Otherwise click finish. That's it. You have successfully create a a new role named 'TestRole' and assigned permission for that. You can see the existing roles by navigating to Home -> Configure -> Users and Roles -> Roles. At this place you can view the permission, delete the role, assign user and also rename the role.
Next we shall see how to create roles and assign any permission in the permission tree to those roles using a programmatic way, without using the UI. You can do that by calling an admin service as an API or using a Java client. I'll outline both the methods here.
There is a web service API called RemoteUserStoreManagerService that can be used to manage users and roles. This is an Admin Service in the WSO2 carbon platform. Admin services in the WSO2 products are hidden by default. To see the wsdl of this web service you need to unhide the Admin service WSDLs. To do that, first, open up CARBON_HOME/repository/conf/carbon.xml and look for the following line.
<HideAdminServiceWSDLs>true</HideAdminServiceWSDLs>Make it to 'false' and restart the server.
After the server is successfully started, you can access the wsdl of the RemoteUserStoreManagerService by navigating to https://localhost:9443/services/RemoteUserStoreManagerService?wsdl (Replace 'localhost' part as applicable).
Following are the two methods I mentioned
1. You can create a SOAP UI project with this wsdl. You can use the addRole method to add the role. A sample SOAP call is given below to add a role named 'ValidRole' and assign permission under '/permission/admin/login/EmailLogin' where 'EmailLogin' being a new permission I created under '/permission/admin/login/'
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.ws.um.carbon.wso2.org" xmlns:xsd="http://dao.service.ws.um.carbon.wso2.org/xsd"> <soapenv:Header/> <soapenv:Body> <ser:addRole> <ser:roleName>ValidRole</ser:roleName> <ser:permissions> <xsd:action>ui.execute</xsd:action> <xsd:resourceId>/permission/admin/login/EmailLogin</xsd:resourceId> </ser:permissions> </ser:addRole> </soapenv:Body> </soapenv:Envelope>2. You can write a Java client instead and invoke the methods in the RemoteUserStoreManagerService. A sample Java program is shown below to achieve this. The very self explanatory. Note: you need to add the plugins directory of a IS product to the classpath of the program to build and run this.
import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.ConfigurationContextFactory; import org.apache.axis2.transport.http.HTTPConstants; import org.apache.axis2.transport.http.HttpTransportProperties; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.um.ws.api.stub.PermissionDTO; import org.wso2.carbon.um.ws.api.stub.RemoteUserStoreManagerServiceStub; public class ISClient { private static final String CARBON_HOME = "/home/shazni/Downloads/WSO2/wso2is-5.0.0"; private static final String SEVER_URL = "https://localhost:9443/services/"; private static final String USER_NAME = "admin"; private static final String PASSWORD = "admin"; private static final String ROLE_NAME = "permissionRole"; public static void main(String args[]){ String trustStore = CARBON_HOME + "/repository/resources/security/wso2carbon.jks"; System.setProperty("javax.net.ssl.trustStore", trustStore ); System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon"); ConfigurationContext configContext; try { configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem( null, null); String serviceEndPoint = SEVER_URL + "RemoteUserStoreManagerService"; RemoteUserStoreManagerServiceStub adminStub = new RemoteUserStoreManagerServiceStub(configContext, serviceEndPoint); ServiceClient client = adminStub._getServiceClient(); Options option = client.getOptions(); option.setProperty(HTTPConstants.COOKIE_STRING, null); HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator(); auth.setUsername(USER_NAME); auth.setPassword(PASSWORD); auth.setPreemptiveAuthentication(true); option.setProperty(org.apache.axis2.transport.http.HTTPConstants.AUTHENTICATE, auth); option.setManageSession(true); boolean authenticate = false; try{ authenticate = adminStub.authenticate(USER_NAME, PASSWORD); } catch (Exception e){ e.printStackTrace(); } if(authenticate){ System.out.println("User is authenticated successfully"); } else { System.err.println("User is authentication failed"); } try{ PermissionDTO permissionDTO = new PermissionDTO(); permissionDTO.setAction(CarbonConstants.UI_PERMISSION_ACTION); permissionDTO.setResourceId("/permission/admin/login/WebLogin"); PermissionDTO[] permissionDTOs = new PermissionDTO[1]; permissionDTOs[0] = permissionDTO ; adminStub.addRole(ROLE_NAME, null, permissionDTOs); System.out.println("Role is created successfully"); } catch (Exception e){ System.err.println("Role creation is failed"); e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } }Well that's it. Hope this article has been informative to you.
Friday, October 17, 2014
Adding new permissions to permission tree in WSO2 Identity Server programmatically
Let's start the WSO2 Identity Server (IS) first. To download and run the WSO2 IS, look here.
The permission tree in WSO2 identity Server is simply, a few collections stored in the underlying registry. Therefore adding new permissions to the permission tree involves adding a meaningful collection underneath a needed permission (collection). All these resources are stored in the registry path; /_system/governance/permission/admin. You can view this permission tree by navigating to an existing roles permission. To do that, navigate to Configure -> Users and Roles -> Roles -> Internal/everyone (Permissions) in the WSO2 IS. In this article I'm gonna write a sample Registry Client to add collections under /_system/governance/permission/admin to add new permissions to the permission tree. There's a permission named /_system/governance/permission/admin/login. We are going to add a permission named WebLogin under this. What needs to happen is simply creating a collection under this collection and assign a property called 'name' with its value being the name that needs to get displayed in the permission tree. This can be easily done using the management console of the WSO2 IS. What if someone needs to do this programmatically? Read further down to see how to!!
Adding a resource or collection under another collection in a product like WSO2 IS involves few works. We can obtain some registry based services from a service client named WSRegistryServiceClient. Retrieving this service and performing operations like get and put on top of Registry instance is the easiest way to manipulate the resources in the registry tree. But this service feature is not shipped with some of the WSO2 products including WSO2 IS by default. Therefore it's required to add this feature to WSO2 IS. Following URL explains how to add a feature to any WSO2 product.
https://docs.wso2.com/display/Carbon420/Installing+Features+via+the+UI
Look for WS API feature under the category of Governance Registry section. Once installed you can use the Registry API's to add collections under the above collection /_system/governance/permission/admin.
Following sample client java program adds the said collection.
Hope this article was useful for any WSO2 product user.
The permission tree in WSO2 identity Server is simply, a few collections stored in the underlying registry. Therefore adding new permissions to the permission tree involves adding a meaningful collection underneath a needed permission (collection). All these resources are stored in the registry path; /_system/governance/permission/admin. You can view this permission tree by navigating to an existing roles permission. To do that, navigate to Configure -> Users and Roles -> Roles -> Internal/everyone (Permissions) in the WSO2 IS. In this article I'm gonna write a sample Registry Client to add collections under /_system/governance/permission/admin to add new permissions to the permission tree. There's a permission named /_system/governance/permission/admin/login. We are going to add a permission named WebLogin under this. What needs to happen is simply creating a collection under this collection and assign a property called 'name' with its value being the name that needs to get displayed in the permission tree. This can be easily done using the management console of the WSO2 IS. What if someone needs to do this programmatically? Read further down to see how to!!
Adding a resource or collection under another collection in a product like WSO2 IS involves few works. We can obtain some registry based services from a service client named WSRegistryServiceClient. Retrieving this service and performing operations like get and put on top of Registry instance is the easiest way to manipulate the resources in the registry tree. But this service feature is not shipped with some of the WSO2 products including WSO2 IS by default. Therefore it's required to add this feature to WSO2 IS. Following URL explains how to add a feature to any WSO2 product.
https://docs.wso2.com/display/Carbon420/Installing+Features+via+the+UI
Look for WS API feature under the category of Governance Registry section. Once installed you can use the Registry API's to add collections under the above collection /_system/governance/permission/admin.
Following sample client java program adds the said collection.
import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.context.ConfigurationContextFactory; import org.wso2.carbon.registry.core.Collection; import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.ws.client.registry.WSRegistryServiceClient; import java.io.File; public class RegistryClient { private static final String CARBON_HOME = "/home/shazni/Downloads/WSO2/wso2is-5.0.0"; private static final String axis2Repo = CARBON_HOME + File.separator + "repository" + File.separator + "deployment" + File.separator + "client"; private static final String axis2Conf = CARBON_HOME + "/repository/conf/axis2/axis2_client.xml"; private static final String username = "admin"; private static final String password = "admin"; private static final String serverURL = "https://localhost:9443/services/"; private static WSRegistryServiceClient initialize() throws Exception { System.setProperty("javax.net.ssl.trustStore", CARBON_HOME + File.separator + "repository" + File.separator + "resources" + File.separator + "security" + File.separator + "wso2carbon.jks"); System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon"); System.setProperty("javax.net.ssl.trustStoreType", "JKS"); System.setProperty("carbon.repo.write.mode", "true"); ConfigurationContext configContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(axis2Repo, axis2Conf); return new WSRegistryServiceClient(serverURL, username, password, configContext); } public static void main(String[] args) throws Exception { Registry registry = initialize(); Collection newCol = registry.newCollection(); newCol.setProperty("name", "WebLogin"); registry.put("/_system/governance/permission/admin/login/WebLogin/", newCol); } }Change the CARBON_HOME variable path to the IS_HOME of your server. Further, to build this you need to add the plugins directory of a Governance Registry product to the class path. Then if you run this code, you get a collection called WebLogin created under /_system/governance/permission/admin
Hope this article was useful for any WSO2 product user.
Sending email from a gmail account using python
In this guide I'll walk you through the python commands that you can use to send an email to someone from your gmail account. If you don't have python set it up in your environment look here for detail. Enter the commands step by step.
You can easily put this into a python script and invoke it and send a quick email to someone from the command line without ever opening your email account in a browser. Following is a sample script which does the same as above.
[shazni@shazniInWSO2 bin]$ python Python 2.7.5 (default, Jun 25 2014, 10:19:55) [GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> fromAddress="aaaaaaaaaaaa@gmail.com" >>> toAddress="bbbbbbbbbbbb@yahoo.com" >>> msg="Subject: Hello\n\nHi!! How are you" >>> import smtplib >>> server=smtplib.SMTP("smtp.gmail.com", 587) >>> server.starttls() (220, '2.0.0 Ready to start TLS') >>> password="xxxxxxxxxxxxxx" >>> server.login(fromAddress, password) (235, '2.7.0 Accepted') >>> server.sendmail(fromAddress, toAddress, msg) {}Replace 'aaaaaaaaaaaa', 'bbbbbbbbbbbb' and 'xxxxxxxxxxxxxx' accordingly.
You can easily put this into a python script and invoke it and send a quick email to someone from the command line without ever opening your email account in a browser. Following is a sample script which does the same as above.
#!/usr/bin/python import smtplib import getpass fromAddress = raw_input("Enter your gmail address: ") toAddress = raw_input("Enter the recipients email address: ") subject = raw_input('Enter the subject of email: ') bodyText = raw_input('Enter the body text: ') msg = "Subject: " + subject + "\n\n" + bodyText #msg="Subject: Hello\n\nHi!! How are you" server=smtplib.SMTP("smtp.gmail.com", 587) server.starttls() password = getpass.getpass('Gmail Password: ') server.login(fromAddress, password) server.sendmail(fromAddress, toAddress, msg)A sample run of the script is shown below.
[shazni@shazniInWSO2 Python]$ python sendEmail.py Enter your gmail address: mshazninazeer@gmail.com Enter the recipients email address: mshazninazeer@yahoo.com Enter the subject of email: Hello Enter the body text: This is a sample text sent to you by python!!! Gmail Password: [shazni@shazniInWSO2 Python]$Enjoy using python script to send emails
Friday, October 10, 2014
Downloading and running WSO2 Identity Server
WSO2 Identity Server (IS) provide means for security and identity management for your web applications, services and API's. In it's latest version (5.0.0) it acts like an Enterprise Identity Bus (an idea of centrally managing the identity regardless of the standards being used). It's a versatile product to manage you application users security, entitle management and many more. See more information about the WSO2 identity server at http://wso2.com/products/identity-server/
How to run WSO2 IS
http://localhost:9443/carbon
You may login with default username (admin) and password (admin). When logged in you would see the management console as shown below.
How to run WSO2 IS
- Download WSO2 IS from above URL itself.
- Extract the zip archive into a directory. Say the extracted dircetory is IS_HOME
- Navigate to the IS_HOME/bin in the console (terminal)
- Enter the following command
$ ./wso2server.sh in Linux or $ ./wso2server.bat in WindowsOnce started you can access the management console by navigating the following URL
http://localhost:9443/carbon
You may login with default username (admin) and password (admin). When logged in you would see the management console as shown below.
Tuesday, September 16, 2014
StoreKeeper 1.0.0 Released
In the current world, lot of things are being sold and bought. The items sold are of various types; be it a consumer good, a service rendered to a client, the providers (sellers) need to keep track of details of their accounting. They, most often also need to provide a bill to the customer. Traditionally, this was a manual process. The provider writes a manual bill and write down the total price with all the items sold with details of quantity, unit price, name etc. In the end of the day he'll calculate his revenue. And also he keeps an eye on the available items to get more items to sell when the items run out of the stock.
What if there is a multipurpose point of sale software, which could do all of the above things in a snap of a time? In the modern world, time is valuable and efficiency in trading to serve more customers is vital for a successful business. Point of sale software are just created for that.
StoreKeeper is a software tool which you can use at the point of sale to add things to transactions and in the end print a bill on a printer (usually using a dot matrix printer). StoreKeeper just makes this process extremely streamlined and is very easy to use. It's a light weight, portable software, which needs a bare minimum of system requirements.
Currently following features are supported.
Features to be added in the future
Following is a screenshot of the StoreKeeper taken from a Linux desktop. It's currently tested to be working in both Windows and Linux.
To give you a small technical idea of StoreKeeper, it's a classic client server application. The server serves request from a Graphical front end GUI client. This means, the StoreKeeper would support multiple clients to use the Software at the same time (This scenario is not yet completely tested).
Following is a sample bill (Sorry for hiding few information) printed using a dot matrix printer.
Like our facebook page for StoreKeeper https://www.facebook.com/StoreKeeperService.
Please feel free to contact us if you wish to purchase this product or if you need to provide your feedback to us via our facebook page https://www.facebook.com/StoreKeeperService.
What if there is a multipurpose point of sale software, which could do all of the above things in a snap of a time? In the modern world, time is valuable and efficiency in trading to serve more customers is vital for a successful business. Point of sale software are just created for that.
StoreKeeper is a software tool which you can use at the point of sale to add things to transactions and in the end print a bill on a printer (usually using a dot matrix printer). StoreKeeper just makes this process extremely streamlined and is very easy to use. It's a light weight, portable software, which needs a bare minimum of system requirements.
Currently following features are supported.
- Add an item
- Update an item
- Query an item or all items to view
- Add users
- Update a user
- Query users
- Add items to transaction
- Delete item from transaction
- Cancel the transaction
- End a transaction
- Print a bill (with a configured printer) together with a pdf copy in the computer
- Get the total sales on a day
Features to be added in the future
- Query current days transactions
- Query current days transaction on a given item/service
- View the history of an item sale
- View the history of total sales
- Notification of different events via email
- Graphs to view the stats
- Different access to different users with different credentials
Following is a screenshot of the StoreKeeper taken from a Linux desktop. It's currently tested to be working in both Windows and Linux.
To give you a small technical idea of StoreKeeper, it's a classic client server application. The server serves request from a Graphical front end GUI client. This means, the StoreKeeper would support multiple clients to use the Software at the same time (This scenario is not yet completely tested).
Following is a sample bill (Sorry for hiding few information) printed using a dot matrix printer.
Like our facebook page for StoreKeeper https://www.facebook.com/StoreKeeperService.
Please feel free to contact us if you wish to purchase this product or if you need to provide your feedback to us via our facebook page https://www.facebook.com/StoreKeeperService.
Monday, September 8, 2014
Having frequent WiFi disconnection in your Linux?
I've been lately using Fedora 20 as my development environment and it's really fascinating. It is one of the best OS's for a developer with extensive support for development no matter what technology you are focused into (Of course if it's Windows or MAC specific, then it's not)
A major issue I have been experiencing in Fedora 20 in my laptop (Lenovo T530), is the frequent WiFi disconnection. It was very embarrassing, although the WiFi signal strength is good. At times the WiFi goes off and never gets connected back. The following was few useful commands I came to know that can force the WiFi adapter to initiate the connection (possibly). I hope this may be useful to some of you as well.
Well I have been doing this for sometime whenever my connection goes off and never re-connects. I found an easy solution ( or rather a workaround ) for frequent disconnection. The workaround was to disable IPv6 in my Linux box. I can't assure the solution would work if you are encountering the same issue. Anyway, that worked for me. Ever since disconnections have been rare.
To disable IPv6 in your machine, do the following.
A major issue I have been experiencing in Fedora 20 in my laptop (Lenovo T530), is the frequent WiFi disconnection. It was very embarrassing, although the WiFi signal strength is good. At times the WiFi goes off and never gets connected back. The following was few useful commands I came to know that can force the WiFi adapter to initiate the connection (possibly). I hope this may be useful to some of you as well.
[shazni@shazniInWSO2 Disk1]$ nmcli -p con ============================================================================================================= List of configured connections ============================================================================================================= NAME UUID TYPE TIMESTAMP-REAL ------------------------------------------------------------------------------------------------------------- TriXNet bd504b18-be5f-4d2d-9ea6-6bff40f29cca 802-11-wireless Mon 08 Sep 2014 01:55:20 PM IST NAUMI LIORA HOTEL dc5a4e03-69f0-4f0b-80a4-b5c6f2755ace 802-11-wireless Thu 21 Aug 2014 10:20:16 PM IST TriXNet_Groud_Floor 6142855d-7845-4f6a-88fb-068b15cbd029 802-11-wireless Thu 14 Aug 2014 11:30:43 PM IST em1 32a2ab29-73af-477a-888c-ab9cf2a489b2 802-3-ethernet never Dialog 4G 3b96c186-012a-43eb-bdb8-72679f318472 802-11-wireless Sun 31 Aug 2014 11:01:30 PM IST PROLiNK_PRN3001 cb54a35f-e459-44cf-a2b3-32071e77460b 802-11-wireless Mon 07 Jul 2014 08:57:24 PM IST ZTE d57d20b1-c556-4706-8849-acafdee1fc88 802-11-wireless Sat 09 Aug 2014 05:50:02 PM IST WSO2 15da72d9-07f9-47bd-9a0a-f055399a6339 802-11-wireless Fri 05 Sep 2014 06:20:15 PM IST AMB 0a521533-22f4-4adc-89c3-8f1b9118743e 802-11-wireless Fri 22 Aug 2014 05:01:37 PM IST [shazni@shazniInWSO2 Disk1]$ nmcli con up uuid bd504b18-be5f-4d2d-9ea6-6bff40f29cca Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)The first command shows the available connections together with device UUIDs. The second command initiates the connection given the UUID. This might result in prompting you for a password.
Well I have been doing this for sometime whenever my connection goes off and never re-connects. I found an easy solution ( or rather a workaround ) for frequent disconnection. The workaround was to disable IPv6 in my Linux box. I can't assure the solution would work if you are encountering the same issue. Anyway, that worked for me. Ever since disconnections have been rare.
To disable IPv6 in your machine, do the following.
[shazni@shazniInWSO2 shazni]$ sudo vi /etc/sysctl.conf3) Add the following line at the end of the file (or where its convenient for you):
net.ipv6.conf.all.disable_ipv6 = 1Restart your machine for the changes to take effect.
Tuesday, July 15, 2014
High Availability Choices for WSO2 Complex Event Processor 3.1.0
The article that I wrote on the subject can be accessed in the following location.
WSO2 Governance Registry: Connectors to the External World
A Webinar content for the subject can be accessed in the following location.
http://wso2.com/library/webinars/2014/07/wso2-governance-registry-connectors-to-the-external-world/
http://wso2.com/library/webinars/2014/07/wso2-governance-registry-connectors-to-the-external-world/
Downloading and running WSO2 Complex Event Processor
WSO2 CEP is a lightweight, easy-to-use, 100% open source Complex Event Processing Server licensed under Apache Software License v2.0. Modern enterprise transactions and activities consists of stream of events. Enterprises that monitor such events in real time and respond quickly to those environments undoubtedly have greater advantage over its competitors. Complex Event Processing is all about listening to such events and detecting patterns in real-time, without having to store those events. WSO2 CEP fulfills this requirements by identifying the most meaningful events within the event cloud, analyzes their impact and acts on them in real-time. It’s extremely high performing and massively scalable.
Once started you can access the management console by navigating the following URL
http://localhost:9443/carbon
How to run WSO2 CEP
- Download WSO2 CEP from http://wso2.com/products/complex-event-processor/
- Extract the zip archive into a directory. Say the extracted dircetory is CEP_HOME
- Navigate to the CEP_HOME/bin in the console (terminal)
- Enter the following command
./wso2server.sh (In Linux)
wso2server.bat (In Windows)Once started you can access the management console by navigating the following URL
http://localhost:9443/carbon
You may login with default username (admin) and password (admin). When logged in you would see the management console as shown below.
Sunday, March 23, 2014
Integrating Sharepoint with WSO2 Governance Registry
WSO2 Governance Registry (GREG) is a fully open source registry-repository for storing and managing service artifacts and other resources.
Microsoft Sharepoint is a web application platform, comprising of multipurpose set of web technologies. Sharepoint has historically been engaged with content and document management. But the latest versions can be used for lot more; document and file management, social networking, websites, intranet, extranet, enterprise search, business intelligence, system integration, process integration and workflow automation. The latest version of Sharepoint is Sharepoint 2013.
In this guide I shall show you how can we integrate Sharepoint resources with WSO2 Governance Registry. This can be useful to govern resources and artifacts stored in Sharepoint in GREG.
In this guide I will create a Sharepoint site and a blog in the site and create a resource in GREG with the blog posts URL.
You can find Sharepoint 2013 installation instructions here.
Let's first create a site in the Sharepoint 2013. You can create a sharepoint site collection using the Sharepoint 2013 Central Administration, which you can find in the start menu. This will prompt you for your Administrative user name and password, which you would have configured at the installation time. Sharepoint 2013 Central Administration window will open up in the browser as shown below.
You can create a site collection by clicking 'Create site collection' link and following the onscreen instructions. I've created a site collection called 'MySiteCollection' for demonstration and I can access the site collection by navigating to the http://win-nh67lj7lsq4/sites/mysites. You can configure your site collections URL while following the above mentioned instructions. When you navigate to your site collection with the configured URL, you would see a similar window as the following.
You can create a new post by clicking 'Create a post' link and following the onscreen instructions. I've created a blog post called 'My first blog post'. After creating you can see the blog post listed in the site collection as shown in the following screenshot.
You can view the blog post by clicking the blog link. Its URL in my case is http://win-nh67lj7lsq4/sites/mysites/Lists/Posts/Post.aspx?ID=3
OK. Now we can import this blog post as a resource in the WSO2 Governance Registry. This would allow us to govern certain aspects of this resource in the WSO2 Governance Registry.
If you haven't downloaded and ran WSO2 Governance Registry, look here for the details. Navigate to the Management console in browser using the URL http://localhost:9443/carbon if you are running the WSO2 Governance Registry in the local machine and running with the default port settings.
Now let's add a resource in the WSO2 Governance Registry, corresponding to the blog post we created in the Sharepoint. Login to the Management Console and click browse and navigate to a path where you want to store the blog post in WSO2 Governance Registry, Let's say in /_system/governace.
Click the 'Add Resource' and select 'import content from URL' for method, as shown in the following picture. Provide the Sharepoint blog post URL and give it a name. This should import the blog post content into WSO2 Governance Registry.
In case you get an error, that is most probably due to Sharepoint resources are protected. You can't access the Sharepoint resources without the authentication provided. Even if you want to access the WSDL in the browser by providing the link, you will be prompted for credentials. So how do we cope this scenario in the WSO2 Governance Registry? WSO2 products offer an option in the configuration to allow this kind of authentication to external resources happen in the network. Open up the carbon.xml located in GREG_HOME/repository/conf. There you will find a tag named <NetworkAuthenticatorConfig>. Provide the following configuration (of course changing the pattern according to your requirement and provide your credentials).
Provide your Sharepoint username and password for the <Username> and <Password> tags. <Pattern> tag allows any URL pattern matching to that to be authenticated in the WSO2 products. Type can be either 'server' or 'proxy' depending on your type.
After doing this change, you need to restart the WSO2 Governance Registry and attempt the above import. Now it should work.
By clicking the resource link the tree view would take you the following screen where you can do all the conventional governance operations for a resource. You can add lifecycles, tags, comments all in the WSO2 Governance Registry.
If you just want to save the blog post as a URI, you may do so by adding a the Sharepoint Blog URL as a URI artifact. This step will further be described below with adding a WSDL.
I'll wrap up this post by adding a WSDL file of a web service stored in the Sharepoint. The WSDL is of a default web service to list the content of a site collection. The WSDL URL of this service name List is http://win-nh67lj7lsq4/sites/mysites/_vti_bin/Lists.asmx?wsdl. Replace win-nh67lj7lsq4/sites/mysites/ with your configured site URL.
Adding a WSDL in WSO2 Governance Registry would import any dependent schema, create a service artifact type and endpoints (if available). Let's create a WSDL artifact in WSO2 Governance Registry.
Click the Add WSDL in the left pane as shown below.
Provide the WSDL URL and a name for it. This would import the WSDL and create any corresponding artifacts. In this case it creates a List service and an endpoint. You can find the service by clicking the Services in the left pane. The endpoint dependency can be seen by clicking the 'View Dependency' in the WSDL list as shown below.
Above description indicated the way it imported the WSDL and its dependencies. You might just want to have the WSDL URL stored in WSO2 Governance Registry and not the imported content. This could be done by adding the WSDL as a URI. For that, click the Add URI button in the left pane. This should bring up a window as shown below.
Provide the WSDL URL for URI. Select WSDL for the Type. Provide the name List.wsdl (provide .wsdl extension anyway) and click save. Now go to the List URI. You should be able to see the WSDL listed there as shown below.
Click the List.wsdl. This will bring up the following windows with the dependency and associations listed in the right side.
This guide gave you a very basic guide on how to integrate some of the resources in Sharepoint with WSO2 Governance Registry. You can do lot more with WSO2 Governance Registry. I recommend you download it and play with the product and get more details from WSO2 official documentation from https://docs.wso2.org/.
Hope this guide was useful.
Cheers
Microsoft Sharepoint is a web application platform, comprising of multipurpose set of web technologies. Sharepoint has historically been engaged with content and document management. But the latest versions can be used for lot more; document and file management, social networking, websites, intranet, extranet, enterprise search, business intelligence, system integration, process integration and workflow automation. The latest version of Sharepoint is Sharepoint 2013.
In this guide I shall show you how can we integrate Sharepoint resources with WSO2 Governance Registry. This can be useful to govern resources and artifacts stored in Sharepoint in GREG.
In this guide I will create a Sharepoint site and a blog in the site and create a resource in GREG with the blog posts URL.
You can find Sharepoint 2013 installation instructions here.
Let's first create a site in the Sharepoint 2013. You can create a sharepoint site collection using the Sharepoint 2013 Central Administration, which you can find in the start menu. This will prompt you for your Administrative user name and password, which you would have configured at the installation time. Sharepoint 2013 Central Administration window will open up in the browser as shown below.
Sharepoint 2013 Central Administration |
You can create a site collection by clicking 'Create site collection' link and following the onscreen instructions. I've created a site collection called 'MySiteCollection' for demonstration and I can access the site collection by navigating to the http://win-nh67lj7lsq4/sites/mysites. You can configure your site collections URL while following the above mentioned instructions. When you navigate to your site collection with the configured URL, you would see a similar window as the following.
Site Collection |
A blog post in Sharepoint |
You can view the blog post by clicking the blog link. Its URL in my case is http://win-nh67lj7lsq4/sites/mysites/Lists/Posts/Post.aspx?ID=3
OK. Now we can import this blog post as a resource in the WSO2 Governance Registry. This would allow us to govern certain aspects of this resource in the WSO2 Governance Registry.
If you haven't downloaded and ran WSO2 Governance Registry, look here for the details. Navigate to the Management console in browser using the URL http://localhost:9443/carbon if you are running the WSO2 Governance Registry in the local machine and running with the default port settings.
Now let's add a resource in the WSO2 Governance Registry, corresponding to the blog post we created in the Sharepoint. Login to the Management Console and click browse and navigate to a path where you want to store the blog post in WSO2 Governance Registry, Let's say in /_system/governace.
Click the 'Add Resource' and select 'import content from URL' for method, as shown in the following picture. Provide the Sharepoint blog post URL and give it a name. This should import the blog post content into WSO2 Governance Registry.
<NetworkAuthenticatorConfig> <Credential> <Pattern>http://192.168.1.9.*</Pattern> <Type>server</Type> <Username>YourUserName</Username> <Password>YourPassword</Password> </Credential> </NetworkAuthenticatorConfig>
Provide your Sharepoint username and password for the <Username> and <Password> tags. <Pattern> tag allows any URL pattern matching to that to be authenticated in the WSO2 products. Type can be either 'server' or 'proxy' depending on your type.
After doing this change, you need to restart the WSO2 Governance Registry and attempt the above import. Now it should work.
By clicking the resource link the tree view would take you the following screen where you can do all the conventional governance operations for a resource. You can add lifecycles, tags, comments all in the WSO2 Governance Registry.
If you just want to save the blog post as a URI, you may do so by adding a the Sharepoint Blog URL as a URI artifact. This step will further be described below with adding a WSDL.
I'll wrap up this post by adding a WSDL file of a web service stored in the Sharepoint. The WSDL is of a default web service to list the content of a site collection. The WSDL URL of this service name List is http://win-nh67lj7lsq4/sites/mysites/_vti_bin/Lists.asmx?wsdl. Replace win-nh67lj7lsq4/sites/mysites/ with your configured site URL.
Adding a WSDL in WSO2 Governance Registry would import any dependent schema, create a service artifact type and endpoints (if available). Let's create a WSDL artifact in WSO2 Governance Registry.
Click the Add WSDL in the left pane as shown below.
Provide the WSDL URL and a name for it. This would import the WSDL and create any corresponding artifacts. In this case it creates a List service and an endpoint. You can find the service by clicking the Services in the left pane. The endpoint dependency can be seen by clicking the 'View Dependency' in the WSDL list as shown below.
Above description indicated the way it imported the WSDL and its dependencies. You might just want to have the WSDL URL stored in WSO2 Governance Registry and not the imported content. This could be done by adding the WSDL as a URI. For that, click the Add URI button in the left pane. This should bring up a window as shown below.
Provide the WSDL URL for URI. Select WSDL for the Type. Provide the name List.wsdl (provide .wsdl extension anyway) and click save. Now go to the List URI. You should be able to see the WSDL listed there as shown below.
Click the List.wsdl. This will bring up the following windows with the dependency and associations listed in the right side.
This guide gave you a very basic guide on how to integrate some of the resources in Sharepoint with WSO2 Governance Registry. You can do lot more with WSO2 Governance Registry. I recommend you download it and play with the product and get more details from WSO2 official documentation from https://docs.wso2.org/.
Hope this guide was useful.
Cheers
Thursday, March 20, 2014
Downloading and running WSO2 API Manager
WSO2 API Manager is fully open source product which allows you to create, publish and manage many aspects of APIs such as life cycle management, versioning, monetization, governance, security etc. A business has high potential for growth in using WSO2 API Manager as it allows managing APIs in a SOA environment in a decentralized manner.
Let's start getting an insight about API Manager by downloading and running it in our local machine
Download the latest WSO2 API Manager from http://wso2.com/products/api-manager/
Extract the WSO2 API Manager into a directory. I'll refer to this directory as APIM_HOME. Navigate in to bin directory and run the WSO2 API Manager as follows.
Open up your favorite browser and open the Management console URL. 9443 is the default port, unless you have changed the offset.
http://localhost:9443/carbon
Note : If you are running more than one WSO2 product at a time, make sure you increment the <Offset> in PRODUCT_HOME/repository/conf/carbon.xml to avoid conflicting ports.
You may be prompted for a security exception in the browser. Add the security exception. You would then be re-directed, after login with default user name (admin) and password (admin) to the API Management console as shown in the following. This is the administrative console use to manage aspects of the API.
WSO2 API Manager also provides two more views, namely, API Publisher and API Store. API Publisher is the place where an API developer would create an API. He/She can also manage the life-cycle of the API in the publisher. API Store is the place where a typical API user would see, available APIs from publisher. He/She can subscribe to APIs and start using it for their own applications. Following two images are the windows of the API publisher and API store respectively in the API Manager 1.6.0.
This is just a startup guide for WSO2 API Manager. I recommend you to play with this product and also to refer to the online documentation for further details.
Cheers
Let's start getting an insight about API Manager by downloading and running it in our local machine
Download the latest WSO2 API Manager from http://wso2.com/products/api-manager/
Extract the WSO2 API Manager into a directory. I'll refer to this directory as APIM_HOME. Navigate in to bin directory and run the WSO2 API Manager as follows.
$ ./wso2server.sh in Linux or $ ./wso2server.bat in WindowsYou would see some content in the console and finally the Managemwnt console url, API publisher URL and the API store URL.
Open up your favorite browser and open the Management console URL. 9443 is the default port, unless you have changed the offset.
http://localhost:9443/carbon
Note : If you are running more than one WSO2 product at a time, make sure you increment the <Offset> in PRODUCT_HOME/repository/conf/carbon.xml to avoid conflicting ports.
You may be prompted for a security exception in the browser. Add the security exception. You would then be re-directed, after login with default user name (admin) and password (admin) to the API Management console as shown in the following. This is the administrative console use to manage aspects of the API.
API Manager Management Console |
WSO2 API Manager also provides two more views, namely, API Publisher and API Store. API Publisher is the place where an API developer would create an API. He/She can also manage the life-cycle of the API in the publisher. API Store is the place where a typical API user would see, available APIs from publisher. He/She can subscribe to APIs and start using it for their own applications. Following two images are the windows of the API publisher and API store respectively in the API Manager 1.6.0.
WSO2 API Manager Publisher |
WSO2 API Manager Store |
This is just a startup guide for WSO2 API Manager. I recommend you to play with this product and also to refer to the online documentation for further details.
Cheers
Tuesday, March 11, 2014
Accessing an existing H2 DB
H2 is one of the fastest file based database available. You can download the database from http://www.h2database.com/
This post is a very short guide on how to access an already existing H2 database through the command line and the browser.
Let me assume, you have a database named test.db in your home (referring it as $HOME). So the full path of your database is $HOME/test.db
First let me explain how to access the DB in command shell.
Extract the downloaded h2 zip file from the above mentioned URL. Extract it to a location of your liking. Navigate to the bin directory of the extracted directory and enter the following.
Now you can enter sql commands against your DB schema.
Now let's see how to access the DB in the Web browser.
Type the following on top the bin directory.
Enter the following,
Saved Settings : Generic H2 (Embedded)
Setting name : Any name you like (You can save or remove this configuration with the save and remove button)
Driver Class : org.h2.Driver
JDBC URL : jdbc:h2:/$HOME/test
Enter also your user name and password for the DB.
You should be able to see a window like the below. Now you can enter sql commands against your DB schema.
Enjoy using H2 DB in your applications.
This post is a very short guide on how to access an already existing H2 database through the command line and the browser.
Let me assume, you have a database named test.db in your home (referring it as $HOME). So the full path of your database is $HOME/test.db
First let me explain how to access the DB in command shell.
Extract the downloaded h2 zip file from the above mentioned URL. Extract it to a location of your liking. Navigate to the bin directory of the extracted directory and enter the following.
$ java -cp h2*.jar org.h2.tools.Shell Welcome to H2 Shell 1.3.174 (2013-10-19) Exit with Ctrl+C [Enter] jdbc:h2:~/test URL jdbc:h2:$HOME/test [Enter] org.h2.Driver Driver org.h2.Driver [Enter] username User username [Enter] password Password password Connected Commands are case insensitive; SQL statements end with ';' help or ? Display this help list Toggle result list / stack trace mode maxwidth Set maximum column width (default is 100) autocommit Enable or disable autocommit history Show the last 20 statements quit or exit Close the connection and exit sql>Replace $HOME with your home directory name. Note at the end you do not specify .db extention as in test.db in the URL. Instead it is just 'test'.
Now you can enter sql commands against your DB schema.
Now let's see how to access the DB in the Web browser.
Type the following on top the bin directory.
$ java -jar h2*.jarA web browser window should be opened as shown below.
Enter the following,
Saved Settings : Generic H2 (Embedded)
Setting name : Any name you like (You can save or remove this configuration with the save and remove button)
Driver Class : org.h2.Driver
JDBC URL : jdbc:h2:/$HOME/test
Enter also your user name and password for the DB.
You should be able to see a window like the below. Now you can enter sql commands against your DB schema.
Enjoy using H2 DB in your applications.
Friday, February 28, 2014
WSO2 ESB Performance Round 7.5
WSO2 has published latest performance round testing of the WSO2 ESB known as WSO2 ESB Performance Round 7.5
Following are some of the stats related to the test conducted with WSO2 ESB 4.8.1.
It indicates relative performance metrics with number of leading open source ESB's.
Numbers clearly indicate WSO2 ESB outperforms other open source ESB's listed.
Following are some of the stats related to the test conducted with WSO2 ESB 4.8.1.
It indicates relative performance metrics with number of leading open source ESB's.
Numbers clearly indicate WSO2 ESB outperforms other open source ESB's listed.
Saturday, February 8, 2014
The history of Unix and Linux
In this post I thought of sharing the history of Unix and Linux in a bullet list for curious people who want to know what has been the history of Unix/Linux in a brief list rather than reading a huge chapter of a classic book.
- The first UNIX implementation was developed in 1969 (the same year that Linus Torvalds was born) by Ken Thompson at Bell Laboratories, a division of the telephone corporation, AT&T. It was written in assembler for a Digital PDP-7 mini-computer
Ken Thomson |
PDP-7 (Which is under reconstruction) |
- In 1970, UNIX was rewritten in assembly language for a newly acquired Digital PDP-11 minicomputer
PDP-11 |
- A short time later, Dennis Ritchie, one of Thompson’s colleagues at Bell Laboratories and an early collaborator on UNIX, designed and implemented the C programming language (The language that almost all the modern languages are derived from). C followed an earlier interpreted language, B. B was initially implemented by Thompson and drew many of its ideas from an even earlier programming language named BCPL
Dennis Richie |
- First edition of Unix - By 1971, UNIX was running on the PDP-11 and already had a FORTRAN compiler and versions of many programs still used today, including ar, cat, chmod, chown, cp, dc, ed, find, ln, ls, mail, mkdir, mv, rm, sh, su, and who.
- Second Edition, June 1972: By this time, UNIX was installed on ten machines within AT&T.
- Unix implemented in C by Ken Thomson and Dennis Richie in 1973
- Third Edition, February 1973: This edition included a C compiler and the first implementation of pipes.
- Fourth Edition, November 1973: This was the first version to be almost totally written in C.
- Fifth Edition, June 1974: By this time, UNIX was installed on more than 50 systems.
- Sixth Edition, May 1975: This was the first edition to be widely used outside AT&T.
- Then, the Unix OS is distributed to Universities with source code.
- January 1979 saw the release of Seventh Edition of UNIX. This release also contained a number of new tools, including awk, make, sed, tar, uucp, the Bourne shell and a FORTRAN 77 compiler. The release of Seventh Edition is also significant because, from this point, UNIX diverged into two important variants: BSD and System V
- Bjarne Stroustrup developed a language which is an extension of C language, originally name as "C with Classes"
Bjarne Stroustrup |
- Many new tools and features were developed at Berkeley, including the C shell, the vi editor, an improved file system (the Berkeley Fast File System), sendmail, a Pascal compiler, and virtual memory management on the new Digital VAX architecture.
- Under the name Berkeley Software Distribution (BSD), this version of UNIX, including its source code, came to be widely distributed. The first full distribution was 3BSD in December 1979. (Earlier releases from Berkeley BSD and 2BSD were distributions of new tools produced at Berkeley, rather than complete UNIX distributions.
- In 1982, breakup of monopoly between AT&T and the US government on telephone system. This led AT&T to market and sell Unix. This resulted in the release of System III (three) in 1981. System III was produced by AT&T’s UNIX Support Group (USG)
- In 1983, "C with Classes" was renamed as C++
- In 1983, the Computer Systems Research Group at the University of California at Berkeley released 4.2BSD. This release was significant because it contained a complete TCP/IP implementation, including the sockets application programming interface (API) and a variety of networking tools. 4.2BSD and its predecessor 4.1BSD became widely distributed within universities around the world. They also formed the basis for SunOS (first released in 1983), the UNIX variant sold by Sun. Other significant BSD releases were 4.3BSD, in 1986, and the final release, 4.4BSD, in 1993.
- The first release of System V (five) followed in 1983, and a series of releases led to the definitive System V Release 4 (SVR4) in 1989, by which time System V had incorporated many features from BSD, including networking facilities. System V was licensed to a variety of commercial vendors, who used it as the basis of their UNIX implementations.
- In addition to the various BSD distributions spreading through academia, by the late 1980s, UNIX was available in a range of commercial implementations on various hardware. These implementations included Sun’s SunOS and later Solaris, Digital’s Ultrix and OSF/1 (nowadays, after a series of renaming and acquisitions, HP Tru64 UNIX), IBM’s AIX, Hewlett-Packard’s (HP’s) HP-UX, NeXT’s NeXTStep, A/UX for the Apple Macintosh, and Microsoft and SCO’s XENIX for the Intel x86-32 architecture.
- In 1985, Richard Stallman founded the Free Software Foundation (FSF), a nonprofit organization to support the GNU project as well as the development of free software in general.
Richard Stallman |
- In 1987, Andrew Tanenbaum, a university professor in Holland created a Unix-like operating system named Minix. This was merely for academic purpose.
Andrew S. Tanenbaum |
- Well-known programs were produced by the GNU project such as Emacs text editor, GCC (originally the GNU C compiler, but now renamed as the GNU compiler collection, comprising compilers for C, C++, and other languages),, the bash shell, and glibc (the GNU C library).
- POSIX.1 (or more specifically POSIX 1003.1) became an IEEE standard in 1988
- Release of GNU General Public License (GPL) in 1989
- FIPS 151-1 was published (Federal Information Processing Standard). This standard required certain optional features of POSIX.1
- In 1989 (XPG3) X/Open Portability Guide Issue 3 was released. This standard was also based on POSIX.
- ANSI C standardization led to C89
- FIPS 151-2 aligned with the 1990 ISO edition of POSIX.1 (FIPS was withdrawn in 2000)
- By 1990, a complete Unix system was ready; except the most important kernel. But, The GNU project had started work on an ambitious kernel design, known as the GNU/HURD, based on the Mach microkernel. But it was far from complete.
- POSIX adaptation for ISO standard in 1990 (ISO/IEC 9945-1:1990)
- Version 2 of the license, released in 1991
- In 1991, Linus Torvalds, creating the Linux kernel, which is inspired by Minix.
Linus Torvalds |
- XPG4 standard was released in 1992
- XPG4 version 2 in 1994 - This incorporated some important parts of AT&T’s System V Interface Definition Issue 3
- By March 1994, the developers were able to release version Linux 1.0. Linux 1.2 appeared in March 1995, Linux 2.0 in June 1996, Linux 2.2 in January 1999, and Linux 2.4 in January 2001. Work on the 2.5 development kernel began in November 2001, and led to the release of Linux 2.6 in December 2003.
- In 1995 Single UNIX Specification (SUSv1) was formulated by combining XPG4 version 2, X/Open Curses Issue 4 version 2 specification, and the X/Open Networking Services (XNS) Issue 4 specification. This was also called UNIX 95 standard.
- In 1996, an Organization name 'The Open Group" was formed by merging X/Open and the Open Software Foundation (OSF). Nearly all key companies in the Unix domain are members of this organization
- SUSv2 in 1997 (This is also occasionally referred to as UNIX 98 and XPG5)
- The first C++ standard was ratified in 1998 and is known as ISO/IEC 14882:1998
- Second C language standard led to C99
- In 2001, POSIX 1003.1-2001 (Also known as SUSv3) in 2001. This then became an ISO standard namely ISO/IEC 9945:2002
- C++ standard was amended with a Technical Corrigendum in 2003 an is known as ISO/IEC 14882:2003
- SUSv4 and POSIX.1-2008 in 2008
- The third C standard was published in 2011. This standard is know as C11
- The next C++ standard was ratified in 2011. This standard is known as C++11
Labels:
Bjarne,
C,
C++,
C++11,
C11,
Dennis Richie,
Linus Torvalds,
Linux,
POSIX,
Stallman,
SUSv3,
Unix
Tuesday, February 4, 2014
Reading XML with Java DOM API
Let's learn how to read an XML file with Java DOM API. There are other API's which we can use to parse and manipulate XML files apart from Java DOM API, such as JAXP. But Java DOM API is very simple and in my opinion any Java developer needs to know DOM API regarless of why he/she need to choose any other API.
Ok.. Let's say we need to parse the following XML file.
documentElement.getElementsByTagName("contact");
Return a NodeList (which is a list of nodes) of all the nodes having the node name "contact". Then we iterate through each such node. And we can take a Node instance from iterating through each element in NodeList. We can determine the node type by getNodeType() method of Node instance. If it's of type ELEMENT_NODE. Node instance is of type Element. Element is a sub type of Node. Therefore we can invoke all the Node methods from Element instance as well.
I recomment you look at Java API documentation for complete reference of the Java DOM API. Hope you find this post a quick start to read XML. Enjoy your day!!!
Ok.. Let's say we need to parse the following XML file.
<?xml version = "1.0"?> <letter> <contact type="sender"> <name> Jane Doe </name> <address1>Box 12345</address1> <address2>15 Any Ave.</address2> <city>Othertown</city> <state>Otherstate</state> <zip>67890</zip> <phone>555-4321</phone> <flag gender="F" age="23"> </flag> </contact> <contact type="receiver"> <name>John Doe</name> <address1>123 Main St.</address1> <address2></address2> <city>Anytown</city> <state>Anystate</state> <zip>12345</zip> <phone>555-1234</phone> <flag gender="M"> </flag> </contact> <salutation>Dear Sir: </salutation> <paragraph>It is our privilege to inform you about our new databasemanaged with XML. This new system allows you to reduce theload on your inventory list server by having the client machineperform the work of sorting and filtering the data. </paragraph> <paragraph>Please visit our website for availability and pricing. </paragraph> <closing>Sincerely, </closing> <signature>Ms. Jane Doe </signature> </letter>Ok... Shall we start looking into Java code that will read content of this XML?
package xmlreading; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XmlReading { public static void main(String[] args) { try { File xmlFile = new File("letter.xml"); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(xmlFile); Element documentElement = document.getDocumentElement() ; documentElement.normalize(); NodeList rootLists = documentElement.getElementsByTagName("contact"); if(rootLists != null && rootLists.getLength() > 0) { for(int k = 0 ; k < rootLists.getLength() ; k++) { Node contactNode = rootLists.item(k); if(contactNode != null && contactNode.getNodeType() == Node.ELEMENT_NODE) { Element nodeElement = (Element) contactNode ; System.out.println("Contact type : " + nodeElement.getAttribute("type")); // This is one way of reading with exact tag name System.out.println(nodeElement.getElementsByTagName("name").item(0).getTextContent()); System.out.println(nodeElement.getElementsByTagName("address1").item(0).getTextContent()); System.out.println(nodeElement.getElementsByTagName("address2").item(0).getTextContent()); System.out.println(nodeElement.getElementsByTagName("city").item(0).getTextContent()); System.out.println(nodeElement.getElementsByTagName("state").item(0).getTextContent()); System.out.println(nodeElement.getElementsByTagName("zip").item(0).getTextContent()); System.out.println(nodeElement.getElementsByTagName("phone").item(0).getTextContent()); // Following wil help read the node name and it's content programmatically NodeList childNodes = contactNode.getChildNodes(); if(childNodes != null && childNodes.getLength() > 0) { for(int j = 0 ; j < childNodes.getLength() ; j++) { Node chileNode = childNodes.item(j); if(chileNode != null && chileNode.getNodeType() == Node.ELEMENT_NODE) { Element contactChileElement = (Element) chileNode ; System.out.println("Contact child : " + chileNode.getNodeName() + " " + chileNode.getTextContent()); } } } } } } } catch (ParserConfigurationException | SAXException ex) { System.err.println("error occurred"); } catch (IOException ex) { System.err.println("error occurred : " + ex); } } }The output of the code is as below.
Contact type : sender Jane Doe Box 12345 15 Any Ave. Othertown Otherstate 67890 555-4321 Contact child : name Jane Doe Contact child : address1 Box 12345 Contact child : address2 15 Any Ave. Contact child : city Othertown Contact child : state Otherstate Contact child : zip 67890 Contact child : phone 555-4321 Contact child : flag Contact type : receiver John Doe 123 Main St. Anytown Anystate 12345 555-1234 Contact child : name John Doe Contact child : address1 123 Main St. Contact child : address2 Contact child : city Anytown Contact child : state Anystate Contact child : zip 12345 Contact child : phone 555-1234 Contact child : flagTo give a short insight of the code, we first create a File instamce with the XML file we need to parse. Then we create a DocumentBuilderFactory instance and then a DocumentBuilder from the factory method newDocumentBuilder(). Then we invoke the parse() method on DocumentBuilder, which will parse the XML and returns a Document object. Line 25 is not really needed, though, it's highly recommended. We can then invoke methods on Element instance; documentElement, to read data out.
documentElement.getElementsByTagName("contact");
Return a NodeList (which is a list of nodes) of all the nodes having the node name "contact". Then we iterate through each such node. And we can take a Node instance from iterating through each element in NodeList. We can determine the node type by getNodeType() method of Node instance. If it's of type ELEMENT_NODE. Node instance is of type Element. Element is a sub type of Node. Therefore we can invoke all the Node methods from Element instance as well.
I recomment you look at Java API documentation for complete reference of the Java DOM API. Hope you find this post a quick start to read XML. Enjoy your day!!!
Subscribe to:
Posts (Atom)