View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.wss4j.stax.test;
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.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * A Callback Handler implementation for the case of signing/encrypting Attachments via the SwA
38   * (SOAP with Attachments) specification.
39   */
40  public class AttachmentCallbackHandler implements CallbackHandler {
41  
42      private final List<Attachment> originalRequestAttachments;
43      private Map<String, Attachment> attachmentMap = new HashMap<>();
44      private List<Attachment> responseAttachments = new ArrayList<>();
45  
46      public AttachmentCallbackHandler(List<Attachment> attachments) {
47          originalRequestAttachments = attachments;
48          if (attachments != null) {
49              for (Attachment attachment : attachments) {
50                  attachmentMap.put(attachment.getId(), attachment);
51              }
52          }
53      }
54  
55      public void handle(Callback[] callbacks)
56          throws IOException, UnsupportedCallbackException {
57          for (Callback callback : callbacks) {
58              if (callback instanceof AttachmentRequestCallback) {
59                  AttachmentRequestCallback attachmentRequestCallback =
60                      (AttachmentRequestCallback) callback;
61  
62                  List<Attachment> attachments =
63                      getAttachmentsToAdd(attachmentRequestCallback.getAttachmentId());
64                  if (attachments.isEmpty()) {
65                      throw new RuntimeException("wrong attachment requested");
66                  }
67  
68                  attachmentRequestCallback.setAttachments(attachments);
69              } else if (callback instanceof AttachmentResultCallback) {
70                  AttachmentResultCallback attachmentResultCallback =
71                      (AttachmentResultCallback) callback;
72                  responseAttachments.add(attachmentResultCallback.getAttachment());
73                  attachmentMap.put(attachmentResultCallback.getAttachment().getId(),
74                                    attachmentResultCallback.getAttachment());
75              } else {
76                  throw new UnsupportedCallbackException(callback, "Unrecognized Callback");
77              }
78          }
79      }
80  
81      public List<Attachment> getResponseAttachments() {
82          return responseAttachments;
83      }
84  
85      // Try to match the Attachment Id. Otherwise, add all Attachments.
86      private List<Attachment> getAttachmentsToAdd(String id) {
87          List<Attachment> attachments = new ArrayList<>();
88          if (attachmentMap.containsKey(id)) {
89              attachments.add(attachmentMap.get(id));
90          } else {
91              if (originalRequestAttachments != null) {
92                  attachments.addAll(originalRequestAttachments);
93              }
94          }
95  
96          return attachments;
97      }
98  
99  }