Java 服务端架构
Spring、Netty、日志框架与工程化实战
🎨 视觉封面Restlet 2.1.4中 匪夷所思的ObjectRepresentation的构造函数
OneCoder使用Restlet最新版2.1.4开发样例,却一直抛出异常:
Exception in thread "main" java.lang.IllegalArgumentException : The serialized representation must have this media type: application/x-java-serialized-object or this one: application/x-java-serialized-object+xml
at org.restlet.representation.ObjectRepresentation.
at org.restlet.representation.Objec tRepresentation.
无论怎么设置MediaType都无效,无奈只能查看次构造函数的源码:
public ObjectRepresentation(Representation serializedRepresentation,
final ClassLoader classLoader) throws IOException,
ClassNotFoundException, IllegalArgumentException {
super(MediaType.APPLICATION_JAVA_OBJECT);
if (serializedRepresentation.getMediaType().equals(
MediaType. APPLICATION_JAVA_OBJECT)) {
if (!VARIANT_OBJECT_BINARY_SUPPORTED ) {
throw new IllegalArgumentException(
"SECURITY WARNING: The usage of ObjectInputStream when "
+ "deserializing binary presentations from unstrusted "
+ "sources can lead to malicious attacks. As pointed "
+ "here (https://github.com/restlet/restlet-framework-java/issues/778), "
+ "the ObjectInputStream class is able to force the JVM to execute unwanted "
+ "Java code. Thus, the support of such format has been disactivated "
+ "by default. You can activate this support by turning on the following system property: "
+ "org.restlet.representation.ObjectRepresentation.VARIANT_OBJECT_BINARY_SUPPORTED." );
}
setMediaType(MediaType.APPLICATION_JAVA_OBJECT );
InputStream is = serializedRepresentation.getStream();
ObjectInputStream ois = null;
if (classLoader != null) {
ois = new ObjectInputStream(is) {
@Override
protected Class<?> resolveClass(
java.io.ObjectStreamClass desc)
throws java.io.IOException,
java.lang.ClassNotFoundException {
return Class
. forName(desc.getName(), false, classLoader);
}
};
} else {
ois = new ObjectInputStream(is);
}
this.object = (T) ois.readObject();
if (is.read() != -1) {
throw new IOException(
"The input stream has not been fully read.");
}
ois.close();
} else if (VARIANT_OBJECT_XML_SUPPORTED
&& serializedRepresentation.getMediaType().equals(
MediaType.APPLICATION_JAVA_OBJECT_XML )) {
if (!VARIANT_OBJECT_XML_SUPPORTED ) {
throw new IllegalArgumentException(
"SECURITY WARNING: The usage of XMLDecoder when "
+ "deserializing XML presentations from unstrusted "
+ "sources can lead to malicious attacks. As pointed "
+ "here (http://blog.diniscruz.com/2013/08/using-xmldecoder-to-execute-server-side.html), "
+ "the XMLDecoder class is able to force the JVM to "
+ "execute unwanted Java code described inside the XML "
+ "file. Thus, the support of such format has been "
+ "disactivated by default. You can activate this "
+ "support by turning on the following system property: "
+ "org.restlet.representation.ObjectRepresentation.VARIANT_OBJECT_XML_SUPPORTED." );
}
setMediaType(MediaType.APPLICATION_JAVA_OBJECT_XML );
InputStream is = serializedRepresentation.getStream();
java.beans.XMLDecoder decoder = new java.beans.XMLDecoder(is);
this.object = (T) decoder.readObject();
if (is.read() != -1) {
throw new IOException(
"The input stream has not been fully read.");
}
decoder.close();
}
throw new IllegalArgumentException(
"The serialized representation must have this media type: "
+ MediaType.APPLICATION_JAVA_OBJECT .toString()
+ " or this one: "
+ MediaType.APPLICATION_JAVA_OBJECT_XML .toString());
}
惊呆的发现,最后的throw new IllegalArgumentException 逻辑被赤裸裸的暴露在外面,也就是不论上面走的是if还是else,最终都会走到这里抛出异常结束。这不免让我一头雾水,回头查看2.1.2版本的源码,发现抛出异常的代码是写在最后的else块里的,这可就大不相同了。
我只能以我目前粗浅的了解,怀疑这是restlet的一个粗心的bug,我已经给restlet发了邮件咨询了该问题,等待回复中。目前,我也只能降到2.1.2版本的restlet进行开发,在2.1.2版上无此问题。
注:已确认是bug。
所有代码开源上传至 GitHub:yummy-code 仓库 · GESP 专题站:GESP WIKI
欢迎加入:C++ GESP/CSP 考级答疑群(688906745) 与 Java/Python交流群(982860385),点击可直接加群。
猜你想读 · 相关文章推荐
Restlet流式读取远端文件内容 InputRepresentation
<p <a href="http://www.coderli.com"OneCoder</a验证用Restlet做服务,读取远端文件内容功能,编写验证代码。目前测试通过,主要是利用restlet内部提供的InputRepresentation对象,通过ReadableByteChannel,按字节流的方式读取文件内容。...
Restlet2.1.6发布,修正ObjectRepresentation的构造函数问题
<p <a href="http://www.coderli.com\"OneCoder</a在Restlet 2.1.4中 匪夷所思的ObjectRepresentation的构造函数中,提到过在使用2.1.4的时候遇到的异常</p <blockquote <p Exception in thread "m...
Restlet 客户端连接超时问题解决
<p 使用Restlet进行同步请求,有时可能处理的时间会很长所以需要客户端进行较长时间的等待。从API中查得客户端的设置方式如下:</p <!--break-- 不过,设置后,OneCoder经测试却发现无效。无论socketTimeout设置为多少。均会在1分钟左右超时。 这好像是由于Restlet默认使用的是一个...
OneCoder (lihongzheshuai)
一个中年人的自留地,记录学习 C++、GESP/NOI、Java、Python 与算法架构的心得体会。本站唯一网址:coderli.com