1.15 Streaming Attachments

Before using the streaming code, verify that you are logged into a GroupWise 8.0 or later POA. After a successful login, the loginResponse contains the POA version in the gwversion element.

The streaming functionality is accessible via the following URL:

http(s)://server:port/attachment?session=...&id=...

  • http or https: HTTP protocol with or without SSL.

  • server: The IP address of the DNS name of the POA server.

  • attachment: The keyword to tell the POA to stream the attachment.

  • session: A valid login session is required.

  • id: The SOAP id of the attachment to be streamed

  • port: The SOAP port.

    There are some additional parameters that can be included in the URL:

  • &plain=1: Returns the document in plain text.

  • &mime=1: Returns the MIME representation (RFC822) of the item. You need to pass in the ID of the item and not the attachment when getting the MIME representation.

A GroupWise error in streaming the attachment is returned as HTTP status 400. The header line contains X-GWError-Code = GroupWise Error Code, which specifies the GroupWise error code.

Sample Java code is given below:

m_item = (Mail)res.getItem();
info = m_item.getAttachments();
items = info.getAttachment();
for ( i = 0; i < items.length; i++ )
{
  str = temp + m_gwMain.getGroupWiseServiceId() + "&id=" + items[i].getId().get_value();
  size = (int)items[ i ].getSize();
  offset = 0;
  try
  {
    URL url = new URL( str );
    HttpURLConnection huc = (HttpURLConnection)url.openConnection();
    InputStream is = huc.getInputStream();
    byte[] b = new byte[ 16384 ];
    out = new FileOutputStream( new File( "c:\\temp", items[ i ].getName() ) );
    System.out.println( "Start: " + new Date().toString() );
    while ( size > 0 )
    {
     len = is.read( b, 0, 16384 );
     if ( -1 == len ) {
     break;
    }
   out.write( b, 0, len );
   offset += len;
   size -= len;
  }
  out.flush();
  out.close();
  out = null;
  System.out.println( "End: " + new Date().toString() );
}
catch ( Exception ex )
{
  ex.printStackTrace();
  break;
 }
}

In GroupWise 8.0 SP2 and later, the ability to stream attachments into GroupWise using HTTP PUT was also added. The format of the URL is similar:

http(s)://server:port/attachment?session=...&id=...&name=...
  • http or https: HTTP protocol with or without SSL.

  • server: The IP address of the DNS name of the POA server.

  • attachment: The keyword to tell the POA to stream the attachment.

  • session: A valid login session is required.

  • id: The SOAP id of the item to receive the streamed attachment.

  • port: The SOAP port.

  • name: The name of the attachment.

A personal attachment can be added to any item. If you want to send an item and stream the attachments to the item, it is a multi-step process. You first create a draft item. You then stream the attachments to the draft item. You then send an item using the sendItemRequest. You supply a link element in the item in the sendItemRequest referencing the draft item. The values are first taken from the item passed in the sendItemRequest. The information from the linked item is then added to the item (including the attachments). After the item is sent, the draft item is purged.

The following Java code shows how to send a large attachment using the streaming code:

  public void testStreamAttachment() {
    byte[] bytes;
    byte[] b = new byte[ 16384 ];
    Calendar end;
    Calendar start;
    Distribution dist = new Distribution();
    File file;
    FileInputStream  in;
    HttpURLConnection huc;
    int code;
    int len;
    LinkInfo link = new LinkInfo();
    Mail mail = new Mail();
    MessageBody mb = new MessageBody();
    MessagePart[]  mp = new MessagePart[1];
    OutputStream  os;
    Recipient[] recip = new Recipient[1];
    RecipientList  list = new RecipientList();
    SendItemResponse resp   = null;
    String id;
    String str;
    URL url;

    try {
      // time the send
      start = Calendar.getInstance();

      // setup a draft item
      mail.setSubject( "Test of HTTP PUT" );
      mail.setSource( ItemSource.draft );

      // get the message body
      bytes = "Message Body!".getBytes( "UTF-8" );
      mp[0] = new MessagePart();
      mp[0].set_value( bytes );
      mb.setPart( mp );
      mail.setMessage( mb );

      // set the recipient
      recip[ 0 ] = new Recipient();
      recip[ 0 ].setDisplayName( "Preston Stephenson" );
      recip[ 0 ].setEmail( "pstephenson@prestons.provo.novell.com" );
      recip[ 0 ].setDistType( DistributionType.TO );
      list.setRecipient( recip );
      dist.setRecipients( list );
      mail.setDistribution( dist );

      // create the draft item
      resp = m_main.getService().sendItemRequest( mail, m_main.getSessionId(),
              m_main.getTrace() );
      if ( 0 != resp.getStatus().getCode() ) {
        m_main.displayError( resp.getStatus(), "testStreamAttachment" );
      }

      // get the draft item id, encode it for the URL
      id = URLEncoder.encode(resp.getId()[0], "UTF-8");

      // set up the URL
      str = "http://" + m_main.getLogin().getServer() + ":" +
          m_main.getLogin().getPort() + "/attachment?session=" +
          m_main.getSessionId() + "&id=" + id + "&name=onemb.txt";
      url = new URL( str );

      // set up the file to stream
      file = new File( "c:\\temp", "onemb.txt" );
      str = String.valueOf( file.length() );
      huc = (HttpURLConnection)url.openConnection();
      huc.setDoOutput( true );
      huc.setRequestMethod( "PUT" );
      huc.setFixedLengthStreamingMode( (int)file.length() );
      os = huc.getOutputStream();
      in = new FileInputStream( file );

      // stream the attachment
      for ( ;; ) {
        len = in.read(b);
        if ( -1 == len ) {
          break;
        }
        os.write( b, 0, len );
      }
      os.close();
      code = huc.getResponseCode();
      if ( code < 200 || code > 299 ) {
        m_main.displayError( resp.getStatus(), "testStreamAttachment" );
      }

      // set up the mail item to send
      mail = new Mail();
      recip[ 0 ] = new Recipient();
      recip[ 0 ].setDisplayName( "Preston Stephenson" );
      recip[ 0 ].setEmail( "pstephenson@prestons.provo.novell.com" );
      recip[ 0 ].setDistType( DistributionType.TO );
      list.setRecipient( recip );
      dist.setRecipients( list );
      mail.setDistribution( dist );

      // set the link to the draft item
      link.setType( LinkType.draft );
      link.setId( resp.getId()[0] );
      mail.setLink( link );

      // send the item
      resp = m_main.getService().sendItemRequest( mail, m_main.getSessionId(),
              m_main.getTrace() );

      // get the elapsed time
      end = Calendar.getInstance();
      str = "Streaming: " + String.valueOf( end.getTimeInMillis() -
            start.getTimeInMillis() );
      m_main.displayError( resp.getStatus(), str );
    } catch ( Exception e ) {
      e.printStackTrace();
    }
  }

NOTE:There is not much of a performance increase for streaming small attachments. You should consider streaming attachments over 64 KB in size.