The Kat's Work - Blog
Main | Blog | Registration | Login
Thursday
2024-05-02
5:57 AM
Welcome Guest | RSS

Much has been made of lotus notes xPages functionality, which is IBMs attempt to scrabble back ground on other web technologies. In my opinion its got a long way to go, before its usable never mind a contender. This push by notes though has meant that another addition to note’s functionality has been widely over looked. This is the ability for notes databases now to provide and consume web services.

Notes has always been jealous of its data, access to it from outside of notes was always a difficult task as was injection of data into it. With the adding of web services this hardship evaporates. Already through this method I have been able to make a Java Spring web application "talk” directly with notes and attached BIRT to allow proper reporting on notes data for the first time ever! I think this is just the tip of the iceberg, and yes its notes so it isn’t perfect yet there are some functions need improving but its an infinite improvement.

To create a notes web services you first need to create a class. This class will be associated with your web services. Functions of the class will become the services operations.


Dim session As NotesSession
Dim db As NotesDatabase

Class CustomersService
Sub New
        Set session = New NotesSession
        Set db = session.CurrentDatabase
    End Sub

    Function sayHello() as string

        SayHello = "Hello”

    End Function
   
End Class

This class will create operation sayHello for your web service. The operation will return a string.

We next need to create the web services provider. Located under the code section of the database in domino designer. In the properties of the provider, we need to set the following:

Port type class CustomerService (i.e the name of the Class)

Port Type Name CustomerService (for wsdl)

Service Element name CustomerService (for wsdl)

Port name Domino (for wsdl)

Your web service will now be available at http://<server>/<path>/<nsf>/<web service> and wsdl at http://<server>/<path>/<nsf>/<web service>?WSDL

More Complicated WebServices

So how do we set up more complicated operations. We can wrap the functions response with other classes. This allows us to return pretty much anything we want.

Class ReportStubs
    Public message As String
    Public reportStubs() As ReportStub
   
End Class

Class ReportStub
   
    Public reportPeriod As String
   
End Class

Dim session As NotesSession
Dim db As NotesDatabase

Class CustomersService
Sub New
        Set session = New NotesSession
        Set db = session.CurrentDatabase
    End Sub

    Function sayHello() as string

        SayHello = "Hello”

    End Function

Function getCustomersAvailableReports(userID As String, securityKey As String, userKey As String) As reportStubs
        Set getUsersAvailableReports = New ReportStubs
        If ( Not Me.checkSecurityKey(securityKey)) Then
           
            getUsersAvailableReports.message = "Access Denied"
            Exit Function
           
        End If
        Dim userConical As String
        userConical = Me.getUserConical(userID,userKey)
        If (userConical = "") Then
           
            getUsersAvailableReports.message = "User Invalid"
           
            Exit Function
           
        End If
        Dim view As NotesView
        Set view = db.GetView("Reports")
        Dim viewEntries As NotesViewEntryCollection
        Dim viewEntry As NotesViewEntry
       
        Set viewEntries = view.GetAllEntriesByKey(userConical ,False)
       
        Set viewEntry = viewEntries.GetFirstEntry
       
        Redim getUsersAvailableReports.reportStubs(0 To (viewEntries.Count-1))
       
        i% = 0
        While Not viewEntry Is Nothing
            Dim reportStub As New ReportStub
           
            reportStub.reportPeriod = viewentry.ColumnValues(1)
           
            Set             getUsersAvailableReports.reportStubs(i%) = reportStub
            i% = i% + 1
            Set viewEntry = viewEntries.GetNextEntry(viewEntry)
        Wend
       
        getUsersAvailableReports.message = "Success"
       
    End Function

Private Function getUserConical(userID As String, userKey As String) As String
       
        getUserConical =  "The Kat"
    '    getUserConical =  ""
    End Function
   
    Private Function checkSecurityKey(securityKey As String)
        If (securityKey = "") Then
           
        End If
        checkSecurityKey = True
       
    End Function
   
End Class

As you can see this will create a operation that will accept a message of there string parameters. The response will be a complex message type as specified by classes ReportStubs and ReportStub.

Views: 2648 | Added by: The_Kat | Date: 2011-03-25 | Comments (0)


I have wrote a rather nifty Java Class that will allow you to specify two locations and find the driving distance between them.

It uses the google api on supplied values. For the best result supply longitude latitude figures (e.g "53.453194,-2.726241") however postcodes will be accepted.


import java.io.DataInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jdom.JDOMException;
import org.jdom.input.DOMBuilder;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class GoogleDistance {
   
    public static Double retrieveMeters(String startLatitudeLongitude, String endLatitudeLongitude) throws Exception{
         
              
               
               
                String version = "2.3";
                String agent = "Mozilla/4.0";
                String respText = "";
                HashMap nvp = null; //lhuynh not used?

                //deformatNVP( nvpStr );
              
                String apiEndPoint = "http://maps.googleapis.com/maps/api/directions/xml";
                sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
             
       

                System.setProperty("java.net.useSystemProxies", "true");


                String data = "origin=" + startLatitudeLongitude + "&destination=" + endLatitudeLongitude + "&sensor=false";
               
                   URL postURL = new URL( apiEndPoint + "?" + data );
                 
                    HttpURLConnection conn = (HttpURLConnection)postURL.openConnection();
                    conn.setConnectTimeout(1000);

                   
            
                    conn.setDoInput (true);
                    conn.setDoOutput (true);
           
                   
                    conn.setRequestProperty( "User-Agent", agent );
            
                 
                    conn.setRequestMethod("GET");

                   
                   
                    // get the output stream to POST to.
             
                 
                   Double meters;
                   // Read input from the input stream.
                    DataInputStream in = new DataInputStream (conn.getInputStream());
                    int rc = conn.getResponseCode();
                 
                    if ( rc != -1)
                    {
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

                        dbf.setValidating(false);
                        dbf.setIgnoringComments(false);
                        dbf.setIgnoringElementContentWhitespace(true);
                        dbf.setNamespaceAware(true);
                   

                        DocumentBuilder docBuilder = null;
                        docBuilder = dbf.newDocumentBuilder();
                        docBuilder.setEntityResolver(new NullResolver());                   

                        org.w3c.dom.Document xmlDoc = docBuilder.parse(in);
                       
                        org.jdom.Document xmlJDoc = GoogleDistance.convert(xmlDoc);
                       
                       
                        org.jdom.Element directionResponse = xmlJDoc.getRootElement();
                        org.jdom.Element route = directionResponse.getChild("route");
                        org.jdom.Element leg = route.getChild("leg");
                        org.jdom.Element distance = leg.getChild("distance");
                        org.jdom.Element value = distance.getChild("value");
                      
                        meters = Double.valueOf(((org.jdom.Text)value.getContent().get(0)).getText());
                       
             
                       

                    return meters;
                    }else{
                        Exception e = new Exception();
                       
                      throw e; 
                    }
                   
                   
                   
                   
                   
                 
                 
         
               
               
               
   

          
       
    }
   
       
   
     public static org.jdom.Document convert( org.w3c.dom.Document document)
        throws JDOMException, IOException {
        // Create new DOMBuilder, using default parser
        DOMBuilder builder = new DOMBuilder();
        org.jdom.Document jdomDoc = builder.build(document);
        return jdomDoc;
    }
}

    class NullResolver implements EntityResolver {
          public InputSource resolveEntity(String publicId, String systemId) throws SAXException,
              IOException {
            return new InputSource(new StringReader(""));
          }
    }


Its also a good example of the use of the HttpURLConnection class to call APIs from a servelet.

As you can see I convert the org.w3c.dom.Document xml document to org.jdom.Document, which I find much simpler to work with.

Views: 4437 | Added by: The_Kat | Date: 2011-03-11 | Comments (1)


Very simple this one but one that has very little support online. In fact has the opposite of support as people suggest truely awful solutions involving javascript or multiple forms.

So here is the situation. You have a form you want two submit buttons, lets say a "Send" and a "Save as Draft" button.

The html page doesn't care what your going to do with the info on the form it just wants to submit it. Your action is awaiting the same information independant of which button is pressed. It perfoms almost the same processes on the data the only difference is that if Send is pressed the form goes through a final submission process. In this example this involves setting a flag on a table to true.

As you can see the multiple forms "solution" isn't an option as the same fields need to be submitted independant of the which one is pressed, and the javascript "solution" is just rubbish. Come on call yourself a programmer.

In this case if we set up the html form correctly it will pass a parameter through that will let our action now which button was pressed.

To set up the form we need to add an id, name and value to each submit button as below.

<input id="save" type="submit" value="save" name="save"/>
<input id="send" type="submit" value="send" name="send"/>


Now when one of these buttons is pressed it will add a parameter of the that name (save or send) to the post request.

In your action you simply have to test for them. As below.

if( request.getParameter("save") != null )
        {
        //Save specific processing
            
        }
        else if( request.getParameter("send") != null )
        {
         //Send specific processing.
       
        }



Views: 13137 | Added by: The_Kat | Date: 2011-02-14 | Comments (3)

« 1 2 3 4 5 6 7 ... 12 13 »
Login form
Adverts
Search
Calendar
«  May 2024  »
SuMoTuWeThFrSa
   1234
567891011
12131415161718
19202122232425
262728293031
Entries archive
Site friends
  • Create your own site
  • Spree4.com
  • My Blog
  • Statistics

    Total online: 1
    Guests: 1
    Users: 0
    spree4
    Copyright MyCorp © 2024
    Website builderuCoz