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.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   * A Callback Handler implementation for the case of signing/encrypting Attachments via the SwA
39   * (SOAP with Attachments) specification or when using xop:Include in the case of MTOM.
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      // Try to match the Attachment Id. Otherwise, add all Attachments.
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 }