Top Banner

ZAppSvr How-To Programming Guide


Here we’re going to show you how to perform various tasks in ZAppSvr. This will include code snippets, methods, and general tips. This document will grow, so be sure to check our website from time to time for more additions.

Sample NetBeans 8.2 Project

Sometimes it's a real advantage to have a project skeleton to get you started. So we've included on here to help kick-start using ZAppSvr.

With this project shell you can rename and extend it, removing what you don't need. There's enough of a skeleton that you can use the debugger to see what's going on and get you started on your new project. This code is just a germination tool but could be really helpful. Modify it any way you like, and remember it is NOT meant to be a tutorial, just assist you in getting off to a good start.

We will be creating developer articles, for those who have purchased ZAppSvr, with topics of greater depth as we move forward. We hope this will help.

You wil need to download an evaluation copy at minimum of ZAppSvr in order to use the project. Use project properties to reference the ZAppSvr.jar and it's companions.

Download SamplePlugin.zip and get moving!

Request Redirection

If you want to redirect a request, or create a new request with data added to the original request, all that is required is to change reqArray[0] to what you want it to be, and leave retData == null. While creating a method is not required, it can simplify the process. Here is a method that you can use, or just look at as an example which illustrates what is required.

private void setRequest(String request)
{
  reqArray[0] = new String("GET /" + request + " HTTP/1.1");
}

As you can see the process is very simple, and does not require the browser to make a redirected request. Since you can add parameters (i.e. ?time=09:08:87&session=7654) to the request, there may be even greater value. For example, you could alter the request to make specialized search engine calls.

Getting a Cookie

If we’re going to use Cookies for a session ID, we will want to get the session id that was passed as a cookie. The following code shows how to do this.

private String getSessionIdFromRequest(String cookieName)
{
  String s = null;
  int beg = 0;
  int end = 0;

  for(int i=1; i < reqArray.length; i++ )
  {
     if( reqArray[i] == null ) // null here indicates no more header items
        return null;
     // Now get the session ID.
     if( reqArray[i].startsWith("Cookie:") )
     {
        beg = reqArray[i].indexOf(cookieName+"=");
        if( beg == -1 )
           return null; // cookie entity (name) did not exist, just return.

        beg = beg+(cookieName.length()+1);

        end = reqArray[i].indexOf(";", beg);
        if(end == -1)
           end = reqArray[i].length();

        s = new String(reqArray[i].substring(beg, end));
        return s;    // returning the cookie value
     }
  }
  return null;
}

This will locate the cookie and return it’s value.

Setting a Cookie

One of the most common things you may need to do, is set a cookie. The following illustrates how easy this is to do with ZAppSvr. All we have to do is add an item to the return header. The following code illustrates how this is done.

private void setCookie(String cookieName, String cookieValue, String expires)
{
  outHdrAdd.addElement("Set-Cookie:"+cookieName+"=" + cookieValue + ";"+
                       " path=/; expires=" + expires);
}

Items added to this vector are added to the response header, so setting a cookie is a breeze.

Returning Dynamic HTTP Properly

If you are returning dynamic data to a browser request, the data must be completely formed. The following method formats the data properly. All that you have to pass in is the HTTP request status and the data to be presented by the browser.

This method is very useful, and you can just drop this code into your Plugin. A similar routine is already available in ZappSvrUtilities, but we include this code here so you can see what is required to return data to a browser. Usually you will just use the existing utility, but the code is instructive.

private void changeRetData( String ststr, byte[] b ) // if okay, status(ststr)="200"
{
  String hdr = null;
  byte[] bout = null;
  byte[] hdrb = null;
  StringBuffer lsb = new StringBuffer();
  int num = 0;
  int z = 0;

  if( b == null )
     b = new byte[0];

  lsb.append("HTTP/1.1 ");
  lsb.append(ststr);
  lsb.append("\r\n");
  lsb.append("Allow: GET, HEAD, POST\r\n");
  lsb.append("Connection: Close\r\n");
  lsb.append("MIME-Version: 1.0\r\n");
  lsb.append("Content-Length: "+b.length+"\r\n");
  lsb.append("Expires: Thu, 01 Dec 1994 16:00:00 GMT\r\n");
  lsb.append("Cache-Control: no-store\r\n");
  lsb.append("Pragma: no-cache\r\n");

  num = outHeaderAdd.size();
  for( z=0; z<num; z++ )
  {
     lsb.append(outHeaderAdd.get(z));
     lsb.append("\r\n");
  }
  lsb.append("\r\n");

  hdr = lsb.toString();
  hdrb = hdr.getBytes();

  if( b != null && b.length > 0 )
  {
     bout = new byte[hdrb.length+b.length];
     System.arraycopy( hdrb, 0, bout, 0, hdrb.length );
     System.arraycopy( b, 0, bout, hdrb.length, b.length );
  }
  else
     bout = hdrb;

  retData = bout;
}
©Copyright 2017, ZeroPoint, LLC.      Questions? Comments? Email Us.