com.equilibrium.util.api
Class MGRequest

java.lang.Object
  extended by com.equilibrium.util.api.MGRequest

public abstract class MGRequest
extends java.lang.Object

This class is used to make MediaGenerator requests. See the MGConnection class for factory methods that create these requests. A functioning sample application similar to that presented below is located in the "sample" folder of the Java API installation.

Example

The following example shows how to request an image generation from MediaRich.

The following MediaScript (sampleScript.ms) is located in the APISampleModule folder. Please see the installation instructions in that folder.

For more information about writing MediaScript see the MediaRich Programmers Guide.

Media Script

  // sampleScript.ms
  // Sample media script to scale an image.
  function scale()
  {
      // get the scale amount and the image.
      var scaleAmount = parseFloat(req.getParameter("amount"));
      var imagePath = req.getFileParamPath(req.getParameter("file"));
      var outputType = req.getParameter("type");
      // your script should check these parameters....
      
      // scale the image
      var m = new Media();
      m.load(name @ imagePath);
      m.scale(xs @ m.getWidth() * scaleAmount, constrain @ true);
      
      // return it.
      m.save(type @ outputType);
  }
 

Java API script invocation

The following code excerpt invokes the preceding script to scale an image (assumed to exist at C:/myImages/testImage.jpg).

  // get a scaled copy of the image at imagePath
  MGConnection conn = null;
  try
  {
      conn = new MGConnection();
      MGRequest req = conn.createRequest();
      req.setScript("sample:/sampleScript.ms");
      req.setFunction("scale");
      req.setParam("amount", "0.5");
      req.setParam("type", "jpeg");
      
      // make a relative path for the path key
      String imagePath = "C:/myImages/testImage.jpg";    // assuming this exists
      String remotePath = "testImage.jpg";    // must be relative
      
      // add the file to the request using a 64K buffer.
      req.addFile("file", imagePath, remotePath, 65536);
      
      // commit the request
      MGResponse resp = req.commit();
      
      if (resp.hasContent())
      {
          // read the content from the resp.
          byte[] content = new byte[resp.getContentLength()];
          resp.readContent(content);
          // do something with the file.
      }
   }
   catch(Exception ex)
   {
      ex.printStackTrace();
   }
   finally
   {
      try { if (conn != null) conn.close(); } catch(Exception ex) {}
   }
   // done
 


Method Summary
 void addFile(java.lang.String filePath, java.lang.String remotePath, int maxBufferSize)
          Add the specified file to the request and associate it with the given remotePath.
 void addFile(java.lang.String key, java.lang.String filePath, java.lang.String remotePath, int maxBufferSize)
          Add the specified file to the request and associate it with the given remotePath.
 void addFileParam(java.lang.String fileName, int len)
          Send a file to the MediaGenerator along with the request.
 void addStringAsFile(java.lang.String value, java.lang.String remotePath)
          Send the specified string to the MediaGenerator as a file parameter.
 void addStringAsFile(java.lang.String key, java.lang.String value, java.lang.String remotePath)
          Send the specified string to the MediaGenerator as a file parameter.
 MGResponse commit()
          Commit the request and return the response.
 void discard()
          Discard the current request.
 void sendFileParamData(byte[] data, int len)
          Send the data associated with the file parameter.
static void setClientType(long clientKey, java.lang.String clientId)
          Set the client type for all instances MGRequest.
 void setFunction(java.lang.String functionName)
          Specify the entry point function within the script.
 void setFunction(java.lang.String scriptPath, java.lang.String functionName)
          Specify the entry point function and script..
 void setParam(java.lang.String key, java.lang.String val)
          Set the script parameter for key to the specified value.
 void setPriority(int priority)
          Set the priority for the request.
 void setScript(java.lang.String scriptPath)
          Designate the path to the script to execute.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setClientType

public static void setClientType(long clientKey,
                                 java.lang.String clientId)
Set the client type for all instances MGRequest. This should be done once at the beginning of the application. If you would like a unique client key for your application please contace equilbrium.

Parameters:
clientKey - the key for this client.
clientId - a unique client id.

setPriority

public void setPriority(int priority)
Set the priority for the request. The request priority indicates which media generator threads are allowed to process requests. Priorities range from 0 (highest priority) to 7 (lowest priority). The 0 priority thread is reserved for status and should not be used. The exact meaning of the other priority values depends on the MediaGenerator setup. Please see the MediaRich Administrator Guide for more information about MediaGenerator priorities.

Parameters:
priority - the priority value.

setParam

public void setParam(java.lang.String key,
                     java.lang.String val)
              throws java.io.IOException
Set the script parameter for key to the specified value. This parameter can be accessed by key from within a script.

Parameters:
key - the key for this parameter.
val - the value for the parameter.
Throws:
java.io.IOException

setScript

public void setScript(java.lang.String scriptPath)
               throws java.io.IOException
Designate the path to the script to execute. This parameter may also be set to an image path if an image is to be loaded without running a script or loaded using one of the imaging reserved parameters (See the MediaRich reference guide).

Parameters:
scriptPath - the MediaGenerator path to the script to execute.
Throws:
java.io.IOException

setFunction

public void setFunction(java.lang.String functionName)
                 throws java.io.IOException
Specify the entry point function within the script. Default is "main".

Parameters:
functionName - the script entry-point function name.
Throws:
java.io.IOException

setFunction

public void setFunction(java.lang.String scriptPath,
                        java.lang.String functionName)
                 throws java.io.IOException
Specify the entry point function and script..

Parameters:
functionName - the script entry-point function name.
Throws:
java.io.IOException

addFileParam

public void addFileParam(java.lang.String fileName,
                         int len)
                  throws java.io.IOException
Send a file to the MediaGenerator along with the request. The path to the file in the media script is accessed by passing fileName to the getFileParamPath method of the request object.

This call must be immediately followed by repeated calls to MGRequest.sendFileParamData(byte[], int) until len bytes have been sent.

Parameters:
fileName - the key name for this file parameter. Must be relative path.
len - the size of file.
Throws:
java.io.IOException

sendFileParamData

public void sendFileParamData(byte[] data,
                              int len)
                       throws java.io.IOException
Send the data associated with the file parameter. This method must be called repeatedly after a call to AddFileParam until the entire file has been sent.

Parameters:
data - an array containing data for the current file parameter.
len - the length of the data to send.
Throws:
java.io.IOException

discard

public void discard()
Discard the current request. After you commit a request if you are not interested in the response call discard. Note that this does not close the connection to the MediaGenerator. It only discards the current request.


commit

public MGResponse commit()
                  throws java.io.IOException,
                         MGException
Commit the request and return the response.

Returns:
the response from the MediaGenerator.
Throws:
java.io.IOException
MGException

addFile

public void addFile(java.lang.String filePath,
                    java.lang.String remotePath,
                    int maxBufferSize)
             throws java.io.IOException
Add the specified file to the request and associate it with the given remotePath. The remotePath is a relative path indicating a cache location on the MediaGenerator. Each file added to a given request must have a unique remotePath.

Parameters:
filePath - the path to a file on the local system.
remotePath - the relative path for the MediaGenerator.
maxBufferSize - the buffer size to use to stream the file.
Throws:
java.io.IOException

addFile

public void addFile(java.lang.String key,
                    java.lang.String filePath,
                    java.lang.String remotePath,
                    int maxBufferSize)
             throws java.io.IOException
Add the specified file to the request and associate it with the given remotePath. The remotePath is a relative path indicating a cache location on the MediaGenerator. Each file added to a given request must have a unique remotePath. Additionally, associate the remote path with the specified request parameter key to facilitate file lookup in the script.

Parameters:
key - the request key to map remote path to.
filePath - the path to a file on the local system.
remotePath - the relative path for the MediaGenerator.
maxBufferSize - the buffer size to use to stream the file.
Throws:
java.io.IOException

addStringAsFile

public void addStringAsFile(java.lang.String value,
                            java.lang.String remotePath)
                     throws java.io.IOException
Send the specified string to the MediaGenerator as a file parameter. The remotePath is a relative path indicating a cache location on the MediaGenerator. Each file added to a given request must have a unique remotePath.

Parameters:
value - the file contents.
remotePath - the relative path for the MediaGenerator.
Throws:
java.io.IOException

addStringAsFile

public void addStringAsFile(java.lang.String key,
                            java.lang.String value,
                            java.lang.String remotePath)
                     throws java.io.IOException
Send the specified string to the MediaGenerator as a file parameter. The remotePath is a relative path indicating a cache location on the MediaGenerator. Each file added to a given request must have a unique remotePath. Additionally, associate the remote path with the specified request parameter key to facilitate file lookup in the script.

Parameters:
value - the file contents.
remotePath - the relative path for the MediaGenerator.
Throws:
java.io.IOException