Service Desk Practitioners Forum
cancel
Showing results for 
Search instead for 
Did you mean: 

Attachment through WEB API

Highlighted
jurkopucik
Super Collector

Attachment through WEB API

Hi everyone....who can help me. I need to save new work order with attachment. This is my code...
String filename="logo.jpg";
IAttachedItemHome attItemHome=SDsession.getAttachedItemHome();
IAttachedItem attItem=attItemHome.openNewAttachedItem();
File f = new File(filename);
attItem.setFilename(filename);
attItem.setBaseName(f.getName());

IEntityInfoHome entInfHome = SDsession.getEntityInfoHome();
IEntityInfo entInfo = entInfHome.openEntityInfo(wo.getItemType());
attItem.setParentEntityType(entInfo);

IAttachment attachment=wo.getAttachment();
attachment.addAttachedItem(attItem);

try {
if (!attachment.getAttachmentExists().booleanValue()) {
attachment.setAttachmentExists(new Boolean(true));
attachment.transfer();
wo.save();
etc....

But it make the error:

Apache Tomcat/4.0.6 - HTTP Status 500 - Internal Server Error

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

type Exception report

message Internal Server Error

description The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.

exception

java.lang.RuntimeException: Error when put file logo.jpg to attachment server: java.io.FileNotFoundException: logo.jpg (No such file or directory)


What can be wrong....?
15 REPLIES

Re: Attachment through WEB API

Hi,
look at this thread

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=211076

I had the same problem but i got the solution after many tries....

I think it's very useful for you
Jasper Verweij
Honored Contributor

Re: Attachment through WEB API

TIM,

As far as I can see your solution only works if webserver and application server are on same machine?

Jasper
jurkopucik
Super Collector

Re: Attachment through WEB API

Thank you TIM...but....
Here are more informations...
I have my own web server with TOMCAT (i thing it's the same like Service Pages on Service Desk Server). Using jsp files to generate html pages and using web-api.jar (SP7). Basically...i want to upload file logo.jsp which is in the same directory like jsp files. But what I really want to do is use html form input type=file tag to insert the file on the client side and transfer it as attachment to Service desk item.
And now...How and where can i use your part of code...?

Re: Attachment through WEB API

Hi,
this code can be used on POST method of your "Search Attachment" page...
On Post method, code inserts in a TreeMap on session file & file's path and transferr it to a web server local path...so when you use API to attach file to SD Object you have to use local path where attachment file was trasferred by previous code....

Briefly, you have to tranfer file to your web server, so you can attach it to any SD object (SC, CI, WO...)

bye bye
jurkopucik
Super Collector

Re: Attachment through WEB API

I use Jasper's question...
Is this working only if you have web server and application server on the same machine...?
I have web server on another machine than application server...
I can transfer the file from client machine to my web server, but if i want to transfer the file from web server to application server as attachment it doesn't work and write previous error.

Re: Attachment through WEB API

hi,
recently i had renew this code using FTP protocol and not copy file....
you simply have to substitute the copy file code to an FTP code...

bye bye
jurkopucik
Super Collector

Re: Attachment through WEB API

what do you mean
"to substitute the copy file code to an FTP code..." ?
I'm the beginner in java and SD. :o)

Re: Attachment through WEB API

we used commons-net-1.2.2.zip founded on apache website to implement an FTP interface class...
It's very simple and useful...
i can't send our solution cause is implemented on an extended class of HP SDClientBean and it's impossible to understand....
Jasper Verweij
Honored Contributor

Re: Attachment through WEB API

TIM,

to complete story:

the following connection are taken into account:

1. browser <--> tomcat webserver
2. tomcat webserver <--> sd application server
3. sd application server <--> sd FTP share

I understand correctly the following happens with your approach:
User adds attachment in browser. Browser uploads via port 80 or 8080 to a directory on tomcat webserver. Tomcat webserver uploads via a FTP to sd application server and tells application server via web-api that it could upload a file. Sd applition server uploads transferred from local share or FTP share to the FTP share of servicedesk (which is available for sd clients).

Am I correct?

Jasper
jurkopucik
Super Collector

Re: Attachment through WEB API

yes Jasper...this is the way i think about...
I can transfer file from client browser to my web server. I can add attachment to item in Service Desk if the file is physicaly on application server. And now i must solve the problem how can i send file via FTP from my web server to application server. I'm going to try what TIM wrote...

Re: Attachment through WEB API

My webserver and app server are on separate boxes.

Here's how I did it. Hopefully it's understandable. I used the commons.net ftp stuff, too. My webapp gets the file from the client, ftp's it to a temp directory on the server (with a "random" file name based on the current time). It then uses the api to attach the file to the support request.

I have a property file that contains the following information:
servicedesk.ftpserver = ourappserver.whatever.com
servicedesk.ftpusername = aUserName
servicedesk.ftppassword = aPassword
# the ftp folder where the files are permanently stored
servicedesk.ftpactualfolder = Attachments
# the ftp folder where files will be temporarily stored
# before being attached to a supportrequest
servicedesk.ftptempattachmentfolder = tempattachmentfolder
# the full path on the ftp server where the files are
# temporarily stored before being attached to
# a support request
servicedesk.ftpabsoluteattachmentfolder = d:\\ftproot\\tempattachmentfolder\\

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

...

IServicecallHome scHome = session.getServicecallHome();
IServicecall call = scHome.openNewServicecall(templateId);

if(file != null && file.getFileName() != null && !file.getFileName().equals("")){
// first, let's write out the file to a temp file on the server
try{
InputStream is = file.getInputStream();
String newName = file.getFileName() + System.currentTimeMillis();

SettingsModel settings = SettingsModel.getInstance();
String ftpServer = settings.findSetting(Globals.PROPS_SD_FTPSERVER);
String ftpUsername = settings.findSetting(Globals.PROPS_SD_FTPUSERNAME);
String ftpPassword = settings.findSetting(Globals.PROPS_SD_FTPPASSWORD);
String ftpFolder = settings.findSetting(Globals.PROPS_SD_FTPTEMPATTACHMENTFOLDER);
String ftpAbsoluteFolder = settings.findSetting(Globals.PROPS_SD_FTPABSOLUTEATTACHMENTFOLDER);

String fullPath = ftpFolder + "/" + newName;
String localFullPath = ftpAbsoluteFolder + newName;

FTPClient client = new FTPClient();
client.connect(ftpServer);
client.login(ftpUsername, ftpPassword);

client.setFileType(FTP.BINARY_FILE_TYPE);
client.storeFile(fullPath, is);

File tempAttachment = new File(localFullPath);

IAttachment attachment = call.getAttachment();

IAttachedItemHome attachedHome = session.getAttachedItemHome();
IAttachedItem attachedFile = attachedHome.openNewAttachedItem();

attachedFile.setFilename(localFullPath);
attachedFile.setBaseName(file.getFileName());

IEntityInfoHome entInfHome = session.getEntityInfoHome();
IEntityInfo entInfo = entInfHome.openEntityInfo(call.getItemType());

attachedFile.setParentEntityType(entInfo);
attachment.addAttachedItem(attachedFile);

if(attachment.getAttachmentExists() == null || !attachment.getAttachmentExists().booleanValue()){
attachment.setAttachmentExists(new Boolean(true));
}

attachment.transfer();
// delete the file we created, if it exists somehow
tempAttachment.delete();

// delete the file we put on the SD server
client.deleteFile(fullPath);
client.disconnect();

} catch(FileNotFoundException e){
System.out.println(e.toString());
throw new ModelUpdateException("Error attaching file. Please try again or submit request without file");
} catch(IOException e){
System.out.println(e.toString());
throw new ModelUpdateException("Error attaching file. Please try again or submit request without file");
}
}
...
Aykut Guven
Collector

Re: Attachment through WEB API

Hello jurko,

I did it before for a SC and i am attaching the releted jsp files. I hope it gives an idea.

Good luck
UPS
jurkopucik
Super Collector

Re: Attachment through WEB API

Hi everybody, thanx for your advises...Finally I solved my problem and it's working.
swamy_2
Acclaimed Contributor

Re: Attachment through WEB API

Hi

I have to do the same task like attaching file through servicepage while creating new service call. can u palese help me by sending your code

Thanks in advance,

Regds,
Swami
ValeriuCraciun
Acclaimed Contributor

Re: Attachment through WEB API

My java application is on the same server as the file, but still I receive the same error.  If I test from the same application the file with the code:

File f = new File(theFilename);
if(f.exists() && !f.isDirectory()) {
  System.out.println("File " + theFilename +" exists!");
}

It is working!

but the following code using web-api is not working:

 

if (attachmentFileName != null ){
IAttachment attachment = sc.getAttachment();

IAttachedItemHome attachedHome = session.getAttachedItemHome();
IAttachedItem attachedFile = attachedHome.openNewAttachedItem();

File f = new File(attachmentFileName);

attachedFile.setFilename(attachmentFileName);
attachedFile.setBaseName(f.getName());

IEntityInfoHome entInfHome = session.getEntityInfoHome();
IEntityInfo entInfo = entInfHome.openEntityInfo(sc.getItemType());

attachedFile.setParentEntityType(entInfo);
attachment.addAttachedItem(attachedFile);

if(attachment.getAttachmentExists() == null || !attachment.getAttachmentExists().booleanValue()){
attachment.setAttachmentExists(new Boolean(true));
}

attachment.transfer();
}

Error when put file d:\test\test.jpg to attachment server: java.io.FileNotFoundException: d:\test\test.jpg (The system cannot find the path specified)

Thank you

Valeriu

//Add this to "OnDomLoad" event