Home   Documentation   Demonstrations   Download

AMICO: Documentation / AMICO Modules / URL Interface

 

An Unified Resource Locator (URL) interface to AMICO. It runs a Web server, enabling other modules to use AMICO commands using HTTP GET request, and in also enables definition of URL adapters, that map variables from the communicator to URLs that are then opened, returned content can optionally be placed in a variable. In this way also REST Web services could be called.

 

NOTE: If you want to open a connection to a resource that returns XML or HTML response and you want to process the response, for example, to extract simpler data from the content based on XPath expressions, you can use AMICO XML Processor.

How To Run

     java -jar %AMICO_HOME%/bin/amico-url.jar [<conf-url>]

 

 

AMICO URL GET Format

To execute AMICO command using URL interface, simply type in your  Web browser:

   

    http://localhost:3380/amico?command=COMMAND

 

3380 is a default port for AMICO URL interface Web server, and it can be changed in the configuration file. Command can be any of the AMICO TCP protocol commands. For example:

    http://localhost:3380/amico?command=UPDATE age 32

    http://localhost:3380/amico?command=DELETE age

    http://localhost:3380/amico?command=GET age

 

See end of this document for an example of how to use this interface from programs.

Configuration

 

A main configuration file:
 

This is a minimal configuration file that uses default parameters for missing attributes.
Use this file if you want to use default settings.

---------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<url>
    <url-adapter url="file:conf/url/test.xml"/>
</url>

This is a full configuration file that overrides default parameters.

---------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<url web-server-port="3380">
    <communicator host="localhost" port="3320">
        <!-- Basic parameter for establishing connection with communicator. Module uses TCP connection.
             <command> element define a list of commands that will be sent to communicator
             each time a module establish a connection (see AMICO TCP protocol for details on commands)
        -->

        <command>UPDATE last-loaded-module URL</command>
        <command>ONUNLOAD DELETE last-loaded-module</command>
    </communicator>


    <url-adapter url="file:conf/url/test.xml"/>
</url>

 

 A configuration file for URL adapter variables (linked from a main configuration file):
 

<?xml version="1.0" encoding="UTF-8"?>
<url-adapter>
 

    <url trigger-variables="web-param" update-variable="web-result">

         <!-- When variable "web-param" change, it will call Google URL with parameter from this variable
               and resulting HTML page will be placed in variable "web-result".
          -->

          http://www.google.nl/search?hl=nl&amp;q=<%=web-param%>

    </url>

    <url trigger-variables="sound-volume">

         <!-- Used for controlling VLC Player. When "sound-volume" changes,
               it will call VLC Player URL interface for setting the volume.
          -->

          http://localhost:8080/?control=volume&amp;value=<%=sound-volume%>

    </url>
</url-adapter>

 

Example of using URL interface from other programs and with AJAX

 

This is simple Java program that uses java.net.URL class to update and read AMICO variables.

 

import java.net.*;

import java.io.*;

public class URLTest {

    public static void main(String[] args) throws Exception {

        String command1 = URLEncoded.encode( "UPDATE variable1 test", "UTF-8" );

        String command2 = URLEncoded.encode( "GET variable1", "UTF-8" );

 

        new URL( "http://localhost:3380/amico?command=" + command1).openStream();

 

        URL communicator = new URL( "http://localhost:3380/amico?command=" + command2);

        BufferedReader in = new BufferedReader( new InputStreamReader( communicator .openStream()));

        String inputLine;

        while ((inputLine = in.readLine()) != null)

            System.out.println( inputLine );
            in.close();

        }

    }

}

 

 

The following example illustrates how Asynchronous JavaScript and XML (AJAX) freamework can be used with AMICO using AMICO URL interface (click here to open this HTML page). Note: to run this example your browser have to access allow script code to access AMICO on the localhost. This may require changing some of security settings. On Internet Explorer this usually does not cause problems. With Firefox, you may need to install the following Greasemonkey script (click here for it, also available here).

 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <script>
        var xmlHttp
        var readVariable


        function updateAmico(variable, value) {
            sendToAmico("
UPDATE " + variable + " " + value);
        }


        function getAmico(variable, textField) {
            return sendToAmico("
GET " + variable);

        }

        function sendToAmico(amicoCommand)
        {
            xmlHttp=GetXmlHttpObject();
            var url="
http://localhost:3380/amico?command=" + amicoCommand;
            xmlHttp.open("GET",url,false);
            xmlHttp.setRequestHeader( "
If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
            xmlHttp.send(null);

            return xmlHttp.responseText;

        }

 

        function amicoReadLoop(variable,elementId)

        {

            amicoReadLoop(variable,elementId,1000);

        }
 

        function amicoReadLoop(variable,elementId,cycleTime)

        {

            readVariable = getAmico( variable );

            if (readVariable != document.getElementById(elementId).value) {

                document.getElementById(elementId).value = readVariable;

            }

            setTimeout("amicoReadLoop('" + variable + "', '" + elementId + "');",cycleTime);

        }


        function GetXmlHttpObject()

        {
            var objXMLHttp=null;
            if (window.XMLHttpRequest) {
                objXMLHttp = new XMLHttpRequest();
            } else if (window.ActiveXObject) {
                objXMLHttp = new ActiveXObject("
Microsoft.XMLHTTP");

            }

            return objXMLHttp;
        }

    </script>
</head>
<body>
    <form>
    Type text: <input type="text" id="txt1" onkeyup="updateAmico('typed-text',this.value)" size="20">
    <hr/>
        Variable <input type="text" id="variable_name" size="20"> =

        <input type="text" id="variable_value" size="20">

        <input type="button" value="Update" onclick="updateAmico(variable_name.value, variable_value.value)">

        <input type="button" value="Get" onclick="variable_value.value=getAmico(variable_name.value)">

        <input type="button" value="Get Loop" onclick="amicoReadLoop(variable_name.value, 'variable_value')">
    </form>
</body>
</html>

 

 

 

Validation Logos