Skip to main content
FORUM PLAN UPDATE: Date revision: This forum will remain open to new posts and responses until December 1, 2018. (After that date, you will still be able to view and search the forum.) Also, we're taking a second look at the best place to host future conversation. For now, keep using this forum, and stay tuned for more news.
 
developerWorks
AIX and UNIX
Information Mgmt
Lotus
New to Lotus
Products
How to buy
Downloads
Live demos
Technical library
Training
Support
Forums & community
Events
Rational
Tivoli
WebSphere
Java™ technology
Linux
Open source
SOA and Web services
Web development
XML
My developerWorks
About dW
Submit content
Feedback



developerWorks  >  Lotus  >  Forums & community  >  Notes/Domino 6 and 7 Forum

Notes/Domino 6 and 7 Forum

developerWorks

  

Sign in to participate PreviousPrevious NextNext

RE: JavaAgent Exception
chris crowley 15.Sep.08 03:16 PM a Web browser
Domino Designer All Releases All Platforms


I've done this and I've gotten it to work in a WebQuerySave Agent. I get the original code from here and worked off of that: http://www.openntf.org/Projects/codebin/codebin.nsf/0/90A96A34D110344386257035005FC39D

I'm trying now to make it work on more than just one image if the document has multiple image attachments. I think I'm stuck making multiple RT fields for the thumbs, one per image.

Let me know if this helps.

//ImageResizeToStream.java
import lotus.domino.Session;
import lotus.domino.Stream;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.ImageFormatException;
import java.awt.image.FilteredImageSource;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import com.sun.jimi.core.filters.AreaAverageScaleFilter;
import com.sun.jimi.core.Jimi;
import java.awt.Image;
//import gnu.regexp;

public class ImageResizeToStream
{
private final boolean DEBUG = true;

public Stream createResizedImage( Session session, InputStream is, int resizedWidth, int resizedHeight) throws Exception
{
try
{
// Get the new image into the decoder
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(is);
// and decode
Image image = decoder.decodeAsBufferedImage();

FilteredImageSource filteredImage = new FilteredImageSource (image.getSource(), new AreaAverageScaleFilter (resizedWidth, resizedHeight));
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Jimi.putImage( "image/jpeg", filteredImage, bout);

// convert bytestream to bytearray
byte [] imageByteArray = bout.toByteArray();

// Populate a NotesStream with the data
Stream stream = session.createStream();
stream.write (imageByteArray);
return stream;
}
catch ( ImageFormatException ife )
{
if ( DEBUG ) ife.printStackTrace();
}
catch ( Exception e )
{
e.printStackTrace();
}
return null;
}
}

//WQS_ImageUpload.java
import lotus.domino.*;
import java.io.PrintWriter;
import java.util.Vector;

public class WQS_ImageUpload extends AgentBase
{
private final boolean DEBUG = true;

private final int THUMBPHOTOWIDTH = 60;
private final int THUMBPHOTOHEIGHT = 60;
private final String RTFIELD = "PhotoThumb";
private final String IMAGENAMEFIELD = "ThumbImageName";
private final String HASTHUMBFIELD = "HasThumb";

private Session session;
private AgentContext agentContext;
private Database db;
private Document doc;
private EmbeddedObject obj;
private String serverName;
private String dbName;
private String docid;

private Stream thumbPhotoStream;

private MIMEEntity photoThumb;

private Vector fieldUpdates = new Vector();

public void NotesMain()
{
try
{
if ( DEBUG ) Utils.debug ( "|");
if ( DEBUG ) Utils.debug ( "/-------------------------------------------------" );
if ( DEBUG ) Utils.debug ( "imageUpload agent started");
if ( DEBUG ) Utils.debug ( "|");

// Get the in-memory document
session = getSession();
agentContext = session.getAgentContext();
db = agentContext.getCurrentDatabase();
doc = agentContext.getDocumentContext();

// Get the image file from the in-memory document
Item file = doc.getFirstItem("$FILE");
if (file != null)
{
String fileName = cleanStringBuffer(file.getValueString());

Vector v = null;
Session session = getSession();

final String fileexts = ".jpg .jpeg";
final String MACRO = "@Contains(\"" + fileName.toUpperCase() + "\"; @Explode(\"" + fileexts.toUpperCase() + "\";\" \"))";
if ( DEBUG ) Utils.debug(MACRO);
boolean hasJPEG = false;
try
{
v = session.evaluate(MACRO);
hasJPEG = (((Double)v.firstElement()).doubleValue() > 0) ? true : false;
}
catch(Exception e)
{
e.printStackTrace();
}

// This is work around because getting Body calling MIMEEntity.getMIMEEntity("Body") doesn't throw an error but
// doesn't write the image out. Could be something to do with headers but haven't investigated further
// doc.removeItem(RTFIELD);

if (hasJPEG)
{
obj = doc.getAttachment(fileName);

ImageResizeToStream irts = new ImageResizeToStream();

// Call the image sizing method of the image resize class here, pass it the obj and the width, should return domino.Stream
thumbPhotoStream = irts.createResizedImage( session, obj.getInputStream(), THUMBPHOTOWIDTH, THUMBPHOTOHEIGHT);

// This is work around because getting Body calling MIMEEntity.getMIMEEntity("Body") doesn't throw an error but
// doesn't write the image out. Could be something to do with headers but haven't investigated further
doc.removeItem(RTFIELD);

// Do not convert MIME to rich text
session.setConvertMIME(false);

// Create the mime entities and set their contents
photoThumb = doc.createMIMEEntity(RTFIELD);

photoThumb.setContentFromBytes( thumbPhotoStream , "image/jpeg", MIMEEntity.ENC_IDENTITY_BINARY );

// Close down the streams
thumbPhotoStream.close();

session.setConvertMIME(true);

// Create URL to redirect back to document
serverName = doc.getItemValueString("server_name");
dbName = db.getFileName();

fieldUpdates.addElement ( new String[] { IMAGENAMEFIELD, fileName } );
fieldUpdates.addElement ( new String[] { HASTHUMBFIELD, "Yes" } );

DocUtilities.updateDocument ( doc, fieldUpdates );
// doc.save();
// Remove the obj
// obj.remove();

}
else
{
if ( DEBUG ) Utils.debug("Photo is wrong type");
doc.removeItem(RTFIELD);
doc.removeItem(IMAGENAMEFIELD);
doc.removeItem(HASTHUMBFIELD);
doc.save();
}
}
else
{
if ( DEBUG ) Utils.debug("Photo not found");
doc.removeItem(RTFIELD);
doc.removeItem(IMAGENAMEFIELD);
doc.removeItem(HASTHUMBFIELD);
doc.save();
}
}
catch ( NotesException ne )
{
ne.printStackTrace();
}
catch( Exception e)
{
e.printStackTrace();
}
finally
{
if ( DEBUG ) Utils.debug ( "|");
if ( DEBUG ) Utils.debug ( "imageUpload agent finished");
if ( DEBUG ) Utils.debug ( "-------------------------------------------------/" );
if ( DEBUG ) Utils.debug ( "|");
}

}
private static String cleanStringBuffer(String target)
{
StringBuffer buf = new StringBuffer();
for(int n=0; n<target.length(); n++)
{
char c = target.charAt(n);
if ((byte)c == 0)
return buf.toString();
else
buf.append(c);
}
return buf.toString();
}
}




JavaAgent Exception (Sam DeNies 29.May.08)
. . RE: JavaAgent Exception (Ken A Collins 29.May.08)
. . . . RE: JavaAgent Exception (Sam DeNies 29.May.08)
. . RE: JavaAgent Exception (Andrei Kouvchin... 1.Jun.08)
. . RE: JavaAgent Exception (chris crowley 15.Sep.08)


Document Options






  Document options
Print this pagePrint this page

Search this forum

Forum views and search


  Forum views and search
Date (threaded)
Date (flat)
With excerpt
Author
Category
Platform
Release
Advanced search

Sign In or Register

 Sign In or Register
Sign in
Forgot your password?
Forgot your user name?
Create new registration

Member Tools


RSS Feeds

 RSS feedsRSS
All forum posts RSS
All main topics RSS
More Lotus RSS feeds

Resources

 Resources
Forum use and etiquette
Native Notes Access
Web site Feedback

Lotus Support

 Lotus Support
IBM Support Portal - Lotus software
Lotus Support documents
Lotus support by product
Lotus support downloads
Lotus support RSS feeds

Wikis

 Wikis
IBM Composite Applications
IBM Mashup Center
IBM Connections
IBM Connections Cloud Developers
IBM Docs
IBM Forms
IBM Mobile Connect
IBM Sametime
IBM SmartCloud for Social Business
IBM Web Experience Factory
Lotus Domino
Lotus Domino Designer
Lotus Expeditor
Lotus Foundations
Lotus iNotes
Lotus Instructor Community Courseware
Lotus Notes
Lotus Notes & Domino Application Development
Lotus Notes Traveler
Lotus Protector
Lotus Quickr
Lotus Symphony
IBM Web Content Manager
WebSphere Portal

Lotus Forums


 Lotus Forums
Notes/Domino 9.0
Notes/Domino 8.5 + Traveler
Notes/Domino XPages development forum
Notes/Domino 8
Notes/Domino 6 and 7
IBM Connections
IBM Mobile Connect
IBM Sametime
IBM SmartCloud Notes
Lotus Enterprise Integration
Lotus Protector
Lotus Quickr
Lotus SmartSuite