1 /* bufferedResponseHandler.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import gnu
.CORBA
.CDR
.cdrBufOutput
;
42 import gnu
.CORBA
.GIOP
.MessageHeader
;
43 import gnu
.CORBA
.GIOP
.ReplyHeader
;
44 import gnu
.CORBA
.GIOP
.RequestHeader
;
45 import gnu
.CORBA
.GIOP
.cxCodeSet
;
47 import org
.omg
.CORBA
.ORB
;
48 import org
.omg
.CORBA
.portable
.OutputStream
;
49 import org
.omg
.CORBA
.portable
.ResponseHandler
;
52 * Provides the CDR output streams for writing the response to the given buffer.
54 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
56 public class bufferedResponseHandler
57 implements ResponseHandler
60 * The message header. This field is used to compute the size and alignments.
61 * It is, however, never directly written to the buffer stream.
63 public final MessageHeader message_header
;
73 public final ReplyHeader reply_header
;
78 public final RequestHeader request_header
;
81 * True if the stream was obtained by invoking {@link #createExceptionReply()},
84 private boolean exceptionReply
;
87 * The buffer to write into.
89 private cdrBufOutput buffer
;
92 * Create a new buffered response handler that uses the given message headers.
93 * The headers are used to compute sizes and check the versions. They are not
94 * written into a stream inside this class.
96 * @param m_header a message header.
97 * @param r_header a reply header.
99 bufferedResponseHandler(ORB an_orb
, MessageHeader m_header
,
100 ReplyHeader r_header
, RequestHeader rq_header
)
102 message_header
= m_header
;
103 reply_header
= r_header
;
104 request_header
= rq_header
;
110 * Get an output stream for providing details about the exception. Before
111 * returning the stream, the handler automatically writes the message header
112 * and the reply about exception header, but not the message header.
114 * @return the stream to write exception details into.
116 public OutputStream
createExceptionReply()
118 exceptionReply
= true;
124 * Get an output stream for writing a regular reply (not an exception).
126 * Before returning the stream, the handler automatically writes the regular
127 * reply header, but not the message header.
129 * @return the output stream for writing a regular reply.
131 public OutputStream
createReply()
133 exceptionReply
= false;
135 reply_header
.reply_status
= ReplyHeader
.NO_EXCEPTION
;
140 * Get the buffer, normally containing the written reply. The reply includes
141 * the reply header (or the exception header) but does not include the message
144 * The stream buffer can also be empty if no data have been written into
145 * streams, returned by {@link #createReply()} or
146 * {@link #createExceptionReply()}.
148 * @return the CDR output stream, containing the written output.
150 public cdrBufOutput
getBuffer()
156 * True if the stream was obtained by invoking {@link #createExceptionReply()},
157 * false otherwise (usually no-exception reply).
159 public boolean isExceptionReply()
161 return exceptionReply
;
165 * Compute the header offset, set the correct version number and codeset.
167 private void prepareStream()
169 buffer
= new cdrBufOutput();
171 buffer
.setVersion(message_header
.version
);
172 buffer
.setCodeSet(cxCodeSet
.find(reply_header
.service_context
));
174 // Since 1.2, the data section is always aligned on the 8 byte boundary.
175 // In older versions, it is necessary to set the offset correctly.
176 if (message_header
.version
.until_inclusive(1, 1))
178 buffer
.setOffset(message_header
.getHeaderSize());
180 // Get the position after the reply header would be written.
181 reply_header
.write(buffer
);
183 int new_offset
= message_header
.getHeaderSize() + buffer
.buffer
.size();
185 buffer
.buffer
.reset();
186 buffer
.setOffset(new_offset
);