1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.wss4j.dom.message;
21
22 import javax.security.auth.callback.Callback;
23 import javax.security.auth.callback.CallbackHandler;
24 import javax.security.auth.callback.UnsupportedCallbackException;
25
26 import org.apache.wss4j.common.ext.Attachment;
27 import org.apache.wss4j.common.ext.AttachmentRequestCallback;
28 import org.apache.wss4j.common.ext.AttachmentResultCallback;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36
37
38
39
40
41 public class AttachmentCallbackHandler implements CallbackHandler {
42
43 private final List<Attachment> originalRequestAttachments;
44 private Map<String, Attachment> attachmentMap = new HashMap<>();
45 private List<Attachment> responseAttachments = new ArrayList<>();
46
47 public AttachmentCallbackHandler() {
48 originalRequestAttachments = Collections.emptyList();
49 }
50
51 public AttachmentCallbackHandler(List<Attachment> attachments) {
52 originalRequestAttachments = attachments;
53 if (attachments != null) {
54 for (Attachment attachment : attachments) {
55 attachmentMap.put(attachment.getId(), attachment);
56 }
57 }
58 }
59
60 public void handle(Callback[] callbacks)
61 throws IOException, UnsupportedCallbackException {
62 for (Callback callback : callbacks) {
63 if (callback instanceof AttachmentRequestCallback) {
64 AttachmentRequestCallback attachmentRequestCallback =
65 (AttachmentRequestCallback) callback;
66
67 List<Attachment> attachments =
68 getAttachmentsToAdd(attachmentRequestCallback.getAttachmentId());
69 if (attachments.isEmpty()) {
70 throw new RuntimeException("wrong attachment requested");
71 }
72
73 attachmentRequestCallback.setAttachments(attachments);
74 } else if (callback instanceof AttachmentResultCallback) {
75 AttachmentResultCallback attachmentResultCallback =
76 (AttachmentResultCallback) callback;
77 responseAttachments.add(attachmentResultCallback.getAttachment());
78 attachmentMap.put(attachmentResultCallback.getAttachment().getId(),
79 attachmentResultCallback.getAttachment());
80 } else {
81 throw new UnsupportedCallbackException(callback, "Unrecognized Callback");
82 }
83 }
84 }
85
86 public List<Attachment> getResponseAttachments() {
87 return responseAttachments;
88 }
89
90
91 private List<Attachment> getAttachmentsToAdd(String id) {
92 List<Attachment> attachments = new ArrayList<>();
93 if (attachmentMap.containsKey(id)) {
94 attachments.add(attachmentMap.get(id));
95 } else {
96 if (originalRequestAttachments != null) {
97 attachments.addAll(originalRequestAttachments);
98 }
99 }
100
101 return attachments;
102 }
103
104 }