Skip to content

Commit

Permalink
backport hessian protocol change from 2.7 (#8432)
Browse files Browse the repository at this point in the history
* backport hessian protocol change from 2.7

* add spi configuration
  • Loading branch information
chickenlj committed Aug 11, 2021
1 parent 5e68e43 commit 86b5e00
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dependencies-bom/pom.xml
Expand Up @@ -102,7 +102,7 @@
<xmemcached_version>1.3.6</xmemcached_version>
<cxf_version>3.1.15</cxf_version>
<thrift_version>0.8.0</thrift_version>
<hessian_version>4.0.38</hessian_version>
<hessian_version>4.0.51</hessian_version>
<servlet_version>3.1.0</servlet_version>
<jetty_version>6.1.26</jetty_version>
<validation_version>1.1.0.Final</validation_version>
Expand Down
Expand Up @@ -24,7 +24,7 @@
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.protocol.AbstractProxyProtocol;

import com.alibaba.dubbo.rpc.protocol.hessian.serialization.Hessian2FactoryUtil;
import com.alibaba.dubbo.rpc.service.GenericService;
import com.alibaba.dubbo.rpc.support.ProtocolUtils;
import com.caucho.hessian.HessianException;
Expand Down Expand Up @@ -122,6 +122,7 @@ protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
int timeout = url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT);
hessianProxyFactory.setConnectTimeout(timeout);
hessianProxyFactory.setReadTimeout(timeout);
hessianProxyFactory.setSerializerFactory(Hessian2FactoryUtil.getInstance().getSerializerFactory());
return (T) hessianProxyFactory.create(serviceType, url.setProtocol("http").toJavaURL(), Thread.currentThread().getContextClassLoader());
}

Expand Down Expand Up @@ -181,7 +182,7 @@ public void handle(HttpServletRequest request, HttpServletResponse response)
}

try {
skeleton.invoke(request.getInputStream(), response.getOutputStream());
skeleton.invoke(request.getInputStream(), response.getOutputStream(), Hessian2FactoryUtil.getInstance().getSerializerFactory());
} catch (Throwable e) {
throw new ServletException(e);
}
Expand Down
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.rpc.protocol.hessian.serialization;

import com.caucho.hessian.io.SerializerFactory;

public abstract class AbstractHessian2FactoryInitializer implements Hessian2FactoryInitializer {
private static SerializerFactory SERIALIZER_FACTORY;

@Override
public SerializerFactory getSerializerFactory() {
if (SERIALIZER_FACTORY != null) {
return SERIALIZER_FACTORY;
}
synchronized (this) {
SERIALIZER_FACTORY = createSerializerFactory();
}
return SERIALIZER_FACTORY;
}

protected abstract SerializerFactory createSerializerFactory();
}
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.rpc.protocol.hessian.serialization;

import com.caucho.hessian.io.SerializerFactory;

public class DefaultHessian2FactoryInitializer extends AbstractHessian2FactoryInitializer {
@Override
protected SerializerFactory createSerializerFactory() {
return new SerializerFactory();
}
}
@@ -0,0 +1,25 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.rpc.protocol.hessian.serialization;

import com.alibaba.dubbo.common.extension.SPI;
import com.caucho.hessian.io.SerializerFactory;

@SPI("default")
public interface Hessian2FactoryInitializer {
SerializerFactory getSerializerFactory();
}
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.rpc.protocol.hessian.serialization;

import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.common.utils.ConfigUtils;
import com.alibaba.dubbo.common.utils.StringUtils;

public class Hessian2FactoryUtil {
static String WHITELIST = "dubbo.application.hessian2.whitelist";
static String ALLOW = "dubbo.application.hessian2.allow";
static String DENY = "dubbo.application.hessian2.deny";
static ExtensionLoader<Hessian2FactoryInitializer> loader = ExtensionLoader.getExtensionLoader(Hessian2FactoryInitializer.class);

public static Hessian2FactoryInitializer getInstance() {
String whitelist = ConfigUtils.getProperty(WHITELIST);
if (StringUtils.isNotEmpty(whitelist)) {
return loader.getExtension("whitelist");
}
return loader.getDefaultExtension();
}
}
@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.rpc.protocol.hessian.serialization;

import com.alibaba.dubbo.common.utils.ConfigUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.caucho.hessian.io.SerializerFactory;

import static com.alibaba.dubbo.rpc.protocol.hessian.serialization.Hessian2FactoryUtil.ALLOW;
import static com.alibaba.dubbo.rpc.protocol.hessian.serialization.Hessian2FactoryUtil.DENY;
import static com.alibaba.dubbo.rpc.protocol.hessian.serialization.Hessian2FactoryUtil.WHITELIST;

/**
* see https://github.com/ebourg/hessian/commit/cf851f5131707891e723f7f6a9718c2461aed826
*/
public class WhitelistHessian2FactoryInitializer extends AbstractHessian2FactoryInitializer {

@Override
public SerializerFactory createSerializerFactory() {
SerializerFactory serializerFactory = new SerializerFactory();
String whiteList = ConfigUtils.getProperty(WHITELIST);
if ("true".equals(whiteList)) {
serializerFactory.getClassFactory().setWhitelist(true);
String allowPattern = ConfigUtils.getProperty(ALLOW);
if (StringUtils.isNotEmpty(allowPattern)) {
serializerFactory.getClassFactory().allow(allowPattern);
}
} else {
serializerFactory.getClassFactory().setWhitelist(false);
String denyPattern = ConfigUtils.getProperty(DENY);
if (StringUtils.isNotEmpty(denyPattern)) {
serializerFactory.getClassFactory().deny(denyPattern);
}
}
return serializerFactory;
}

}
@@ -0,0 +1,2 @@
default=com.alibaba.dubbo.rpc.protocol.hessian.serialization.DefaultHessian2FactoryInitializer
whitelist=com.alibaba.dubbo.rpc.protocol.hessian.serialization.WhitelistHessian2FactoryInitializer

0 comments on commit 86b5e00

Please sign in to comment.