01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
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: */package org.apache.cxf.interceptor;
19:
20: import java.io.IOException;
21: import java.util.ResourceBundle;
22:
23: import org.apache.cxf.attachment.AttachmentSerializer;
24: import org.apache.cxf.common.i18n.BundleUtils;
25: import org.apache.cxf.message.Message;
26: import org.apache.cxf.message.MessageUtils;
27: import org.apache.cxf.phase.AbstractPhaseInterceptor;
28: import org.apache.cxf.phase.Phase;
29:
30: public class AttachmentOutInterceptor extends
31: AbstractPhaseInterceptor<Message> {
32:
33: public static final String WRITE_ATTACHMENTS = "write.attachments";
34:
35: private static final ResourceBundle BUNDLE = BundleUtils
36: .getBundle(AttachmentOutInterceptor.class);
37:
38: private AttachmentOutEndingInterceptor ending = new AttachmentOutEndingInterceptor();
39:
40: public AttachmentOutInterceptor() {
41: super (Phase.PRE_STREAM);
42: }
43:
44: public void handleMessage(Message message) {
45:
46: boolean mtomEnabled = MessageUtils
47: .isTrue(message
48: .getContextualProperty(org.apache.cxf.message.Message.MTOM_ENABLED));
49: boolean writeAtts = MessageUtils.isTrue(message
50: .getContextualProperty(WRITE_ATTACHMENTS));
51:
52: if (!mtomEnabled && !writeAtts) {
53: return;
54: }
55:
56: AttachmentSerializer serializer = new AttachmentSerializer(
57: message);
58: serializer.setXop(mtomEnabled);
59:
60: try {
61: serializer.writeProlog();
62: } catch (IOException e) {
63: throw new Fault(new org.apache.cxf.common.i18n.Message(
64: "WRITE_ATTACHMENTS", BUNDLE), e);
65: }
66: message.setContent(AttachmentSerializer.class, serializer);
67:
68: // Add a final interceptor to write attachements
69: message.getInterceptorChain().add(ending);
70: }
71:
72: public class AttachmentOutEndingInterceptor extends
73: AbstractPhaseInterceptor<Message> {
74: public AttachmentOutEndingInterceptor() {
75: super (Phase.PRE_STREAM_ENDING);
76: }
77:
78: public void handleMessage(Message message) {
79: AttachmentSerializer ser = message
80: .getContent(AttachmentSerializer.class);
81: if (ser != null) {
82: try {
83: ser.writeAttachments();
84: } catch (IOException e) {
85: throw new Fault(
86: new org.apache.cxf.common.i18n.Message(
87: "WRITE_ATTACHMENTS", BUNDLE), e);
88: }
89: }
90: }
91:
92: }
93: }
|