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.mina.filter.codec.serialization;
21
22 import java.io.NotSerializableException;
23 import java.io.Serializable;
24
25 import org.apache.mina.core.buffer.IoBuffer;
26 import org.apache.mina.core.session.IoSession;
27 import org.apache.mina.filter.codec.ProtocolEncoder;
28 import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
29 import org.apache.mina.filter.codec.ProtocolEncoderOutput;
30
31 /**
32 * A {@link ProtocolEncoder} which serializes {@link Serializable} Java objects
33 * using {@link IoBuffer#putObject(Object)}.
34 *
35 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
36 */
37 public class ObjectSerializationEncoder extends ProtocolEncoderAdapter {
38 private int maxObjectSize = Integer.MAX_VALUE; // 2GB
39
40 /**
41 * Creates a new instance.
42 */
43 public ObjectSerializationEncoder() {
44 // Do nothing
45 }
46
47 /**
48 * Returns the allowed maximum size of the encoded object.
49 * If the size of the encoded object exceeds this value, this encoder
50 * will throw a {@link IllegalArgumentException}. The default value
51 * is {@link Integer#MAX_VALUE}.
52 */
53 public int getMaxObjectSize() {
54 return maxObjectSize;
55 }
56
57 /**
58 * Sets the allowed maximum size of the encoded object.
59 * If the size of the encoded object exceeds this value, this encoder
60 * will throw a {@link IllegalArgumentException}. The default value
61 * is {@link Integer#MAX_VALUE}.
62 */
63 public void setMaxObjectSize(int maxObjectSize) {
64 if (maxObjectSize <= 0) {
65 throw new IllegalArgumentException("maxObjectSize: " + maxObjectSize);
66 }
67
68 this.maxObjectSize = maxObjectSize;
69 }
70
71 public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
72 if (!(message instanceof Serializable)) {
73 throw new NotSerializableException();
74 }
75
76 IoBuffer buf = IoBuffer.allocate(64);
77 buf.setAutoExpand(true);
78 buf.putObject(message);
79
80 int objectSize = buf.position() - 4;
81 if (objectSize > maxObjectSize) {
82 throw new IllegalArgumentException("The encoded object is too big: " + objectSize + " (> " + maxObjectSize
83 + ')');
84 }
85
86 buf.flip();
87 out.write(buf);
88 }
89 }