-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathUploadInitializationRequestElement.java
More file actions
168 lines (146 loc) · 7.22 KB
/
UploadInitializationRequestElement.java
File metadata and controls
168 lines (146 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (c) 1990-2012 kopiLeft Development SARL, Bizerte, Tunisia
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package org.kopi.ebics.xml;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Calendar;
import org.apache.commons.codec.binary.Base64;
import org.apache.xmlbeans.XmlObject;
import org.kopi.ebics.client.EbicsParams;
import org.kopi.ebics.exception.EbicsException;
import org.kopi.ebics.interfaces.ContentFactory;
import org.kopi.ebics.interfaces.EbicsOrderType;
import org.kopi.ebics.io.Splitter;
import org.kopi.ebics.schema.h005.BTUOrderParamsDocument;
import org.kopi.ebics.schema.h005.StandardOrderParamsType;
import org.kopi.ebics.schema.h005.StaticHeaderOrderDetailsType;
import org.kopi.ebics.session.EbicsSession;
import org.kopi.ebics.utils.Utils;
/**
* The <code>UInitializationRequestElement</code> is the common initialization
* element for all ebics file uploads.
*
*
*/
public class UploadInitializationRequestElement extends InitializationRequestElement {
/**
* Constructs a new <code>UInitializationRequestElement</code> for uploads initializations.
* @param session the current ebics session.
* @param orderType the upload order type
* @param userData the user data to be uploaded
*/
public UploadInitializationRequestElement(EbicsSession session, EbicsOrderType orderType,
EbicsParams params,
byte[] userData) {
super(session, orderType, generateName(orderType));
setSaveSuggestedPrefixes("urn:org:ebics:H005", "");
this.userData = userData;
splitter = new Splitter(userData);
this.uploadParams = params;
}
@Override
public void buildInitialization() throws EbicsException {
userSignature = new UserSignature(session.getUser(), generateName("UserSignature"),
session.getConfiguration().getSignatureVersion(), userData);
userSignature.build();
userSignature.validate();
splitter.readInput(session.getConfiguration().isCompressionEnabled(), keySpec);
var mutable = EbicsXmlFactory.createMutableHeaderType("Initialisation", null);
var product = EbicsXmlFactory.createProduct(session.getProduct().getLanguage(),
session.getProduct().getName());
var authentication = EbicsXmlFactory.createAuthentication(
session.getConfiguration().getAuthenticationVersion(),
"http://www.w3.org/2001/04/xmlenc#sha256",
decodeHex(session.getUser().getPartner().getBank().getX002Digest()));
var encryption = EbicsXmlFactory.createEncryption(
session.getConfiguration().getEncryptionVersion(),
"http://www.w3.org/2001/04/xmlenc#sha256",
decodeHex(session.getUser().getPartner().getBank().getE002Digest()));
var bankPubKeyDigests = EbicsXmlFactory.createBankPubKeyDigests(authentication, encryption);
String nextOrderId = uploadParams.getOrderId();
var type = StaticHeaderOrderDetailsType.AdminOrderType.Factory.newInstance();
type.setStringValue(this.getType());
var orderParamsType = (XmlObject) EbicsXmlFactory.createStandardOrderParamsType();
var orderParamsSchema = StandardOrderParamsType.type;
if (uploadParams.getOrderParams() != null) {
var p = uploadParams.getOrderParams();
orderParamsType = EbicsXmlFactory.createBTUParams(p.getServiceName(), p.getScope(),
p.getOption(), p.getMessageName(), p.getMessageVersion(), p.isSignatureFlag());
orderParamsSchema = BTUOrderParamsDocument.type;
}
StaticHeaderOrderDetailsType orderDetails = EbicsXmlFactory.createStaticHeaderOrderDetailsType(
nextOrderId, type, orderParamsType,orderParamsSchema);
var xstatic = EbicsXmlFactory.createStaticHeaderType(session.getBankID(), nonce,
splitter.getSegmentNumber(), session.getUser().getPartner().getPartnerId(), product,
session.getUser().getSecurityMedium(), session.getUser().getUserId(),
Calendar.getInstance(), orderDetails, bankPubKeyDigests);
var header = EbicsXmlFactory.createEbicsRequestHeader(true, mutable, xstatic);
var encryptionPubKeyDigest = EbicsXmlFactory.createEncryptionPubKeyDigest(
session.getConfiguration().getEncryptionVersion(),
"http://www.w3.org/2001/04/xmlenc#sha256",
decodeHex(session.getUser().getPartner().getBank().getE002Digest()));
var signatureData = EbicsXmlFactory.createSignatureData(true,
Utils.encrypt(Utils.zip(userSignature.prettyPrint()), keySpec));
var dataEncryptionInfo = EbicsXmlFactory.createDataEncryptionInfo(true,
encryptionPubKeyDigest, generateTransactionKey());
String digest;
try {
// TODO: check if this is correct
digest = Base64.encodeBase64String(MessageDigest.getInstance("SHA-256", "BC").digest(this.userData));
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
throw new EbicsException(e);
}
var dataTransfer = EbicsXmlFactory.createDataTransferRequestType(dataEncryptionInfo,
signatureData, digest);
var body = EbicsXmlFactory.createEbicsRequestBody(dataTransfer);
var request = EbicsXmlFactory.createEbicsRequest(session.getConfiguration().getRevision(),
session.getConfiguration().getVersion(), header, body);
document = EbicsXmlFactory.createEbicsRequestDocument(request);
}
/**
* Returns the user signature data.
* @return the user signature data.
*/
public UserSignature getUserSignature() {
return userSignature;
}
/**
* Returns the content of a given segment.
* @param segment the segment number
* @return the content of the given segment
*/
public ContentFactory getContent(int segment) {
return splitter.getContent(segment);
}
/**
* Returns the total segment number.
* @return the total segment number.
*/
public int getSegmentNumber() {
return splitter.getSegmentNumber();
}
// --------------------------------------------------------------------
// DATA MEMBERS
// --------------------------------------------------------------------
private final EbicsParams uploadParams;
private final byte[] userData;
private UserSignature userSignature;
private final Splitter splitter;
private static final long serialVersionUID = -8083183483311283608L;
}