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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
public int searchBooksAWS(String index,String motCle) throws Exception{
try
{
AWSECommerceService service = new AWSECommerceService();
service.setHandlerResolver(new AwsHandlerResolver("Y"));
AWSECommerceServicePortType port = service.getAWSECommerceServicePort();
ItemSearchRequest request = new ItemSearchRequest();
request.setSearchIndex(index);
request.setKeywords(motCle);
ItemSearch search = new ItemSearch();
search.setAWSAccessKeyId("X");
search.getRequest().add(request);
Holder<OperationRequest> operation_request = null;
Holder<List<Items>> items = new Holder<List<Items>>();
port.itemSearch(search.getMarketplaceDomain() != null ? search.getMarketplaceDomain() : "",
search.getAWSAccessKeyId() != null ? search.getAWSAccessKeyId() : "",
search.getAssociateTag() != null ? search.getAssociateTag() : "",
search.getXMLEscaping() != null ? search.getXMLEscaping() : "",
search.getValidate() != null ? search.getValidate() : "",
search.getShared() != null ? search.getShared() : null,
search.getRequest() != null ? search.getRequest() : null,
operation_request,
items);
for(Items i : items.value)
{
System.out.println("Nombre de resultats : " + i.getTotalResults().intValue());
System.out.println("Nombre de pages : " + i.getTotalPages().intValue());
for(Item it : i.getItem()){
System.out.println(it.getItemAttributes().getTitle());
}
}
}
catch(Exception e)
{
throw e;
}
return ServiceEtat.Etat.catalogue.ordinal();
}
class AwsHandlerResolver implements HandlerResolver {
private String awsSecretKey;
private List<Handler> handlerChain;
public AwsHandlerResolver(String awsSecretKey) {
this.awsSecretKey = awsSecretKey;
}
public String getAwsSecretKey() {
return awsSecretKey;
}
public void setAwsSecretKey(String awsSecretKey) {
this.awsSecretKey = awsSecretKey;
}
public List<Handler> getHandlerChain() {
return handlerChain;
}
public void setHandlerChain(List<Handler> handlerChain) {
this.handlerChain = handlerChain;
}
@SuppressWarnings("unchecked")
public List<Handler> getHandlerChain(PortInfo portInfo) {
handlerChain = new ArrayList<Handler>();
QName serviceQName = portInfo.getServiceName();
if(serviceQName.getLocalPart().equals("AWSECommerceService")) {
handlerChain.add(new AwsHandler(this.awsSecretKey));
}
return handlerChain;
}
}
public class AwsHandler implements SOAPHandler<SOAPMessageContext> {
private byte[] secretBytes;
private Node firstChild;
private final static Logger logger = Logger.getLogger(com.projet.library.business.AwsHandler.class.getName());
public AwsHandler(String awsSecretKey) {
secretBytes = stringToUtf8(awsSecretKey);
}
public Node getFirstChild() {
return firstChild;
}
public void setFirstChild(Node firstChild) {
this.firstChild = firstChild;
}
public void close(MessageContext messagecontext) {
logger.info("close");
}
public Set<QName> getHeaders() {
return null;
}
public boolean handleFault(SOAPMessageContext messagecontext) {
logger.info("handleFault");
try
{
messagecontext.getMessage().writeTo(System.out);
}
catch(SOAPException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
return true;
}
public boolean handleMessage(SOAPMessageContext messagecontext) {
Boolean outbound = (Boolean) messagecontext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound != null && outbound == true) {
try {
SOAPMessage soapMessage = messagecontext.getMessage();
SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();
SOAPBody soapBody = envelope.getBody();
this.firstChild = soapBody.getFirstChild();
String timeStamp = getTimestamp();
String signature = getSignature(firstChild.getLocalName(), timeStamp, secretBytes);
appendTextElement(firstChild, "Signature", signature);
appendTextElement(firstChild, "Timestamp", timeStamp);
soapMessage.saveChanges();
} catch(SOAPException se) {
throw new RuntimeException("SOAPException was thrown.", se);
}
catch(SOAPException se){
throw new RuntimeException("SOAPException was thrown.", se);
}
}
return true;
}
private void appendTextElement(Node node, String elementName, String elementText) {
Element element = node.getOwnerDocument().createElement(elementName);
element.setTextContent(elementText);
node.appendChild(element);
}
private String getSignature(String operation, String timeStamp, byte[] secretBytes) {
try {
String toSign = operation + timeStamp;
byte[] toSignBytes = stringToUtf8(toSign);
Mac signer = Mac.getInstance("HmacSHA256");
SecretKeySpec keySpec = new SecretKeySpec(secretBytes, "HmacSHA256");
signer.init(keySpec);
signer.update(toSignBytes);
byte[] signBytes = signer.doFinal();
String signature = new String(Base64.encodeBase64(signBytes));
return signature;
} catch(NoSuchAlgorithmException nsae) {
throw new RuntimeException("NoSuchAlgorithmException was thrown.", nsae);
} catch(InvalidKeyException ike) {
throw new RuntimeException("InvalidKeyException was thrown.", ike);
}
}
private String getTimestamp() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat.format(calendar.getTime());
}
private byte[] stringToUtf8(String source) {
try {
return source.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// This will never happen. UTF-8 is always available.
throw new RuntimeException("getBytes threw an UnsupportedEncodingException", e);
}
}
} |
Partager