Loading...

Java PaaS

Getting started on building your first app

Chintana Wilamuna - WSO2 Inc.

Press key to advance.

Registering a domain

  • Register a domain name by going to StratosLive.com
  • Fill out the registration forms
  • Activate your account by clicking the link on the verify email
  • Your domain will be a tenant in StratosLive
  • Login with your credentials

What we're going to build?

  • We're going to develop a simple Twitter like application
  • 3 basic functionalities - post, profile view, public timeline
  • Profile view is tweets by a particular user
  • Let's call it (terrible name alert) .. Jwitter

Create a DB

We'll create a database for our app to use

Go to Data Services Server
Add a database
Select type
Give a DB name
DB created

Create DB user

Now we'll create a DB user that'll do CRUD operations

Adding a privileged group
Adding new group
Select privileged options
Save group
Group is listed
Back to the DB list
Click Manage and add a user
Adding a user
Fill up user details
User created successfully
Explore Database
Explore database screen
Creating a table
create table message (
	id integer auto_increment primary key,
	user varchar(50),
	message varchar(140)
);          	
          	
Execute table creation SQL

Create a data service

Let's create a data service to access our database

Create a data service
Data service info
Adding a data source
Add new data source
Test connection
Add Query
Proceed to create a query
Query for getting all users
Adding an output mapping
Add columns
After adding all 3 columns
Save the query
Proceed to adding an operation
Add new operation
Operation info
Operation added
Click finish to deploy the data service
Recap
  • We created a database for our app
  • Created a table
  • Created a data service
  • Added an operation to get all users from the table
  • When you access this operation user data in the table will be available as an XML document
Next step
  • Now we're going to add a operation to insert messages to the table
  • This requires having an input mapping to get user details
  • Click the data service name in the services list and proceed
Let's go through the wizard
Add query for insert
Add input mapping
Add column details
Add message column as well
Save query
Add insert operation
New operation info
Click finish to save
Next operation
  • We have a data service which can insert a user and get all messages
  • Next add an operation to get all messages by a user
  • Define an input mapping and an output mapping
  • Go through the wizard as previously and try it out
Data service configuration
   <query id="getMessagesByUserQuery" useConfig="JwitterDataSource">
      <sql>select * from jwitter where user = :user</sql>
      <result element="messages" rowName="m">
         <element name="id" column="id" xsdType="xs:integer" />
         <element name="user" column="user" xsdType="xs:string" />
         <element name="message" column="message" xsdType="xs:string" />
      </result>
      <param name="user" sqlType="STRING" />
   </query>

   <operation name="getMessagesByUser">
      <call-query href="getMessagesByUserQuery">
         <with-param name="user" query-param="user" />
      </call-query>
   </operation>          	
          	

If you feel lazy just copy and paste the above configuration to your data service configuration. Keep the other configuration entries.

Creating a webapp

Let's now create a webapp that'll talk to our data service

Creating a webapp with Eclipse
Create a new Carbon Application
Name your application
Open root-artifact.xml
Create a new Web Application
Create a new Dynamic Web Project
Name your web project
Webapp created, click Finish
Webapp created
Recap
  • We created a new Carbon Application
  • Under that, we created a new web app
  • You can create other Carbon artifacts like Data Services, Registry resources etc...
  • Gives a nice way to bundle resources associated with your application
Next step
  • We're going to code generate and create Java stubs for calling our data service
  • I'm going to use the command line tool wsdl2java.sh to do that
  • Then copy the created class files to the src folder in your webapp
Goto WSDL1.1 link in your data service
Copy URL of the WSDL
Codegen and copy
wsdl2java -u -uw -uri http://data.stratoslive.com/services/t/engwar.com/JwitterDS?wsdl
Refresh IDE
Setting up libraries
  • Now we need to setup couple of libraries for our app to work
  • These libraries are available in any release of WSO2 Carbon based product
Copy axis2 library into WEB-INF/lib
Setup build path libraries
Adding sample records
  • Our table is empty
  • Let's add a few sample records using our data service
Click Try this service link
Add a few records
Back to our app

Configure deployment descriptor for form based auth

    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>Jwitter Auth</realm-name>
        <form-login-config>
            <form-login-page>/profile.jsp</form-login-page>
            <form-error-page>/error.jsp</form-error-page>
        </form-login-config>
    </login-config>

    <security-role>
        <role-name>everyone</role-name>
    </security-role>
    <security-role>
        <role-name>admin</role-name>
    </security-role>

    <security-constraint>
        <display-name>Jwitter Security Constraint</display-name>
        <web-resource-collection>
            <web-resource-name>Protected Area</web-resource-name>
            <url-pattern>/profile.jsp</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>          
          
DD configured
Downloading the application
  • It's a good time to download the application, open it up on Eclipse and see the JSP files
  • Within the next couple of slides let's go through bits that talk to the data service
Public timeline
Tweets by user
Deploying the application
  • Let's deploy our application in Stratos
  • We'll be using WSO2 Stratos Application server to deploy our app
  • First we have to export the Carbon Application as a .car file (Carbon Archive)
Export CAR file
Click Finish and save CAR file
Goto Application Server
Click Add under Applications
Select CAR file and upload
CApp uploaded
CApp listing
Goto web applications list, Go To URL
Our webapp
Recap
  • We created a CAR file which contained our webpp
  • Then we deployed this Carbon Application into Stratos App Server
  • App Server then deploys our webapp which is inside the CAR file
Things to note
  • Where's our users?! - They're taken from your tenant's user configuration
  • Add several users to your tenant and login as different users, add couple of tweets
  • Only users in your domain will be able to login

Thanks!