001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.filter.codec.serialization; 021 022import java.io.Serializable; 023 024import org.apache.mina.core.buffer.BufferDataException; 025import org.apache.mina.core.buffer.IoBuffer; 026import org.apache.mina.core.session.IoSession; 027import org.apache.mina.filter.codec.CumulativeProtocolDecoder; 028import org.apache.mina.filter.codec.ProtocolDecoder; 029import org.apache.mina.filter.codec.ProtocolDecoderOutput; 030 031/** 032 * A {@link ProtocolDecoder} which deserializes {@link Serializable} Java 033 * objects using {@link IoBuffer#getObject(ClassLoader)}. 034 * 035 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 036 */ 037public class ObjectSerializationDecoder extends CumulativeProtocolDecoder { 038 private final ClassLoader classLoader; 039 040 private int maxObjectSize = 1048576; // 1MB 041 042 /** 043 * Creates a new instance with the {@link ClassLoader} of 044 * the current thread. 045 */ 046 public ObjectSerializationDecoder() { 047 this(Thread.currentThread().getContextClassLoader()); 048 } 049 050 /** 051 * Creates a new instance with the specified {@link ClassLoader}. 052 * 053 * @param classLoader The class loader to use 054 */ 055 public ObjectSerializationDecoder(ClassLoader classLoader) { 056 if (classLoader == null) { 057 throw new IllegalArgumentException("classLoader"); 058 } 059 this.classLoader = classLoader; 060 } 061 062 /** 063 * @return the allowed maximum size of the object to be decoded. 064 * If the size of the object to be decoded exceeds this value, this 065 * decoder will throw a {@link BufferDataException}. The default 066 * value is <tt>1048576</tt> (1MB). 067 */ 068 public int getMaxObjectSize() { 069 return maxObjectSize; 070 } 071 072 /** 073 * Sets the allowed maximum size of the object to be decoded. 074 * If the size of the object to be decoded exceeds this value, this 075 * decoder will throw a {@link BufferDataException}. The default 076 * value is <tt>1048576</tt> (1MB). 077 * 078 * @param maxObjectSize The maximum size for an object to be decoded 079 */ 080 public void setMaxObjectSize(int maxObjectSize) { 081 if (maxObjectSize <= 0) { 082 throw new IllegalArgumentException("maxObjectSize: " + maxObjectSize); 083 } 084 085 this.maxObjectSize = maxObjectSize; 086 } 087 088 /** 089 * {@inheritDoc} 090 */ 091 @Override 092 protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { 093 if (!in.prefixedDataAvailable(4, maxObjectSize)) { 094 return false; 095 } 096 097 out.write(in.getObject(classLoader)); 098 return true; 099 } 100}