- 追加された行はこの色です。
- 削除された行はこの色です。
RIGHT:[[目次>Hestia Hacks]]|[[前>RJOによるServiceContentの参照]]|[[次>RJOによる受信した添付ファイルの保存]]
目次
#contents
*概要 [#g71443a5]
アクションメッセージの送信時における、添付ファイルの設定方法について解説します。
サンプルプログラム全体を以下に示します。
【PIPSenderAttach.java】
1 : import java.io.File;
2 : import java.io.FileInputStream;
3 : import java.util.Date;
4 : import java.util.Properties;
5 :
6 : import com.ados.hestia.parser.rnbm.access.Validator;
7 : import com.ados.hestia.parser.rnbm.exchange.Container;
8 : import com.ados.hestia.parser.rnbm.exchange.PayloadContainer;
9 : import com.ados.hestia.parser.rnbm.exchange.StatusContainer;
10 : import com.ados.hestia.parser.rnbm.model.RootElement;
11 : import com.ados.hestia.parser.rnbm.particle.Attachment;
12 : import com.ados.hestia.parser.rnbm.particle.DateTimeStamp;
13 : import com.ados.hestia.parser.rnbm.particle.ServiceContent;
14 : import com.ados.hestia.parser.rnbm.repository.Repository;
15 : import com.ados.hestia.sdk.Configuration;
16 : import com.ados.hestia.sdk.Connection;
17 : import com.ados.hestia.sdk.message.DirectMessageExchanger;
18 : import com.ados.hestia.sdk.rnbm.randomgenerator.RootElementGenerator;
19 : import com.ados.hestia.sdk.session.InitiationSession;
20 : import com.ados.hestia.sdk.session.Session;
21 : import com.ados.hestia.utils.UID;
22 :
23 : public class PIPSenderAttach {
24 :
25 : public static void main(String[] args) {
26 : try {
27 : Properties prop = new Properties();
28 : prop.load(new FileInputStream("sample.properties"));
29 : String HOST = prop.getProperty("HOST");
30 : String USER = prop.getProperty("USER");
31 : String PASS = prop.getProperty("PASS");
32 : String HANDLER_ID = prop.getProperty("HANDLER_ID");
33 : String SENDER = prop.getProperty("SENDER");
34 : String RECEIVER = prop.getProperty("RECEIVER");
35 : String DIR = System.getProperty("user.dir") + File.separator
36 : + "input" + File.separator + "attach";
37 :
38 : Connection connection = new Connection(HOST, USER, PASS,
39 : Connection.EXCHANGE_MODE_DIRECT, null, null, null,
40 : HANDLER_ID);
41 : DirectMessageExchanger exchanger = new DirectMessageExchanger(
42 : connection, Configuration.getInstance());
43 :
44 : RootElement re = Repository.getDefault().getResolver()
45 : .resolveByVersion("0C1",
46 : "Pip0C1AsynchronousTestNotification", "R01.01.00");
47 : RootElementGenerator reg = new RootElementGenerator(re);
48 : reg.process();
49 : ServiceContent sc = new ServiceContent("0C1", re);
50 : sc.setGlobalDocumentFunctionCode("Request");
51 : sc.setDocumentGenerationDateTime(new DateTimeStamp());
52 : sc.setDocumentIdentifier((new UID()).toString());
53 : sc.setElementValue(
54 : "fromRole/PartnerRoleDescription/PartnerDescription/BusinessDescription/GlobalBusinessIdentifier",
55 : SENDER);
56 : sc.setElementValue(
57 : "toRole/PartnerRoleDescription/PartnerDescription/BusinessDescription/GlobalBusinessIdentifier",
58 : RECEIVER);
59 :
60 : Session session = new InitiationSession(new PayloadContainer(
61 : new Date(), SENDER, RECEIVER, (new UID()).toString(), sc,
62 : null));
63 : SampleUtils.printSessionState("created", session);
64 : PayloadContainer request = session.getMessageToSend();
65 : for (int i = 0; i < args.length; i++) {
66 : String filename = args[i];
67 : Attachment att = new Attachment(filename, null,
68 : new FileInputStream(DIR + File.separator + filename));
69 : att.setContentID((new UID()).toString());
70 : request.addAttachment(att);
71 : SampleUtils.println("Attachment : " + att.toString() + ",CID:"
72 : + att.getContentID());
73 : }
74 : SampleUtils.printSessionState("got", session);
75 : SampleUtils.printContainerInformation(request);
76 : SampleUtils.printServiceContent(request.getServiceContent());
77 : new Validator(request.getServiceContent().getElement()).validate();
78 : exchanger.sendMessage(request);
79 : SampleUtils.printSessionState("sent", session);
80 :
81 : int time = 0;
82 : boolean loop = true;
83 : while (loop) {
84 : Thread.sleep(5000);
85 : exchanger.hit();
86 : SampleUtils.printSessionState("loop=" + time++, session);
87 :
88 : Container container = exchanger.receiveMessage();
89 : if (container != null) {
90 : SampleUtils.printContainerInformation(container);
91 : session.pushStatus((StatusContainer) container);
92 : SampleUtils.printSessionState("pushed", session);
93 : loop = false;
94 : }
95 : }
96 :
97 : } catch (Exception e) {
98 : e.printStackTrace();
99 : }
100 : }
101 :
102 : }
*Attachmentの利用 [#d7fa0bbf]
ファイル名とInputStreamを指定して、Attachmentを生成します。Attachmentコンストラクタの第2パラメータはコンテントタイプ(application/pdf等)ですが、nullを入力すると、ファイル名の拡張子を判別して自動的にコンテントタイプを設定します。
67 : Attachment att = new Attachment(filename, null,
68 : new FileInputStream(DIR + File.separator + filename));
添付ファイルのコンテントIDを、明示的に指定することができます。ServiceContentに添付ファイルのコンテントIDを設定する必要がある場合は、ServiceContentとAttachmentに同一のコンテントIDを入力します。
69 : att.setContentID((new UID()).toString());
PayloadContainerにAttachmentを入力します。複数のAttachmentを入力できます。
70 : request.addAttachment(att);
|関連クラス|機能|h
|com.ados.hestia.parser.rnbm.particle.Attachment|添付ファイルを表すクラス。|
|com.ados.hestia.utils.UID|ユニークなIDを表すクラス。|