Skip to content

Commit

Permalink
Merge pull request dangdangdotcom#19 from li-shen/master
Browse files Browse the repository at this point in the history
add support for dubbo rest clients in webapp deployment
  • Loading branch information
li-shen committed Dec 29, 2014
2 parents 0e6afd7 + 306372b commit 30ac5cf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.remoting.http.HttpBinder;
import com.alibaba.dubbo.remoting.http.servlet.BootstrapListener;
import com.alibaba.dubbo.remoting.http.servlet.ServletManager;
import com.alibaba.dubbo.rpc.RpcException;
import com.alibaba.dubbo.rpc.protocol.AbstractProxyProtocol;
import com.alibaba.dubbo.rpc.protocol.ServiceImplHolder;
Expand All @@ -40,6 +42,7 @@
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine;
import org.jboss.resteasy.util.GetRestful;

import javax.servlet.ServletContext;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.WebApplicationException;
import java.util.Collections;
Expand Down Expand Up @@ -87,8 +90,30 @@ protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcExcept
servers.put(addr, server);
}

String contextPath = getContextPath(url);
if ("servlet".equalsIgnoreCase(url.getParameter(Constants.SERVER_KEY, "jetty"))) {
ServletContext servletContext = ServletManager.getInstance().getServletContext(ServletManager.EXTERNAL_SERVER_PORT);
if (servletContext == null) {
throw new RpcException("No servlet context found. Since you are using server='servlet', " +
"make sure that you've configured " + BootstrapListener.class.getName() + " in web.xml");
}
String webappPath = servletContext.getContextPath();
if (StringUtils.isNotEmpty(webappPath)) {
webappPath = webappPath.substring(1);
if (!contextPath.startsWith(webappPath)) {
throw new RpcException("Since you are using server='servlet', " +
"make sure that the 'contextpath' property starts with the path of external webapp");
}
contextPath = contextPath.substring(webappPath.length());
if (contextPath.startsWith("/")) {
contextPath = contextPath.substring(1);
}
}
}

final Class resourceDef = GetRestful.getRootResourceClass(implClass) != null ? implClass : type;
server.deploy(resourceDef, impl, getContextPath(url));

server.deploy(resourceDef, impl, contextPath);

final RestServer s = server;
return new Runnable() {
Expand Down Expand Up @@ -199,8 +224,7 @@ public void destroy() {
}

protected String getContextPath(URL url) {
// TODO better solution?
int pos = url.getPath().indexOf("/");
int pos = url.getPath().lastIndexOf("/");
return pos > 0 ? url.getPath().substring(0, pos) : "";
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<module>dubbo-admin</module>
<module>dubbo-demo</module>
<module>hessian-lite</module>
<module>dubbo-tool/dubbo-demo-lite-archetype</module>
<!--<module>dubbo-tool/dubbo-demo-lite-archetype</module>-->
</modules>
<profiles>
<profile>
Expand Down

0 comments on commit 30ac5cf

Please sign in to comment.