diff --git a/apps/showcase/pom.xml b/apps/showcase/pom.xml index 67921e43b..6b8d6c1f0 100644 --- a/apps/showcase/pom.xml +++ b/apps/showcase/pom.xml @@ -89,6 +89,11 @@ struts2-bean-validation-plugin + + org.apache.struts + struts2-async-plugin + + javax.servlet servlet-api diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/async/AsyncFilter.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/async/AsyncFilter.java new file mode 100644 index 000000000..95d98ca53 --- /dev/null +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/async/AsyncFilter.java @@ -0,0 +1,53 @@ +/* + * 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 org.apache.struts2.showcase.async; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; + +/** + * Filters async actions directly to Struts servlet + */ +public class AsyncFilter implements Filter { + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + String requestURI = ((HttpServletRequest) servletRequest).getRequestURI(); + if (!requestURI.contains("/async/receiveNewMessages")) { + filterChain.doFilter(servletRequest, servletResponse); // Just continue chain. + } else { + servletRequest.getRequestDispatcher("/async/receiveNewMessages").forward(servletRequest, servletResponse); + } + } + + @Override + public void destroy() { + + } +} diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/async/ChatRoomAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/async/ChatRoomAction.java new file mode 100644 index 000000000..5877fe6e1 --- /dev/null +++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/async/ChatRoomAction.java @@ -0,0 +1,68 @@ +/* + * 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 org.apache.struts2.showcase.async; + +import com.opensymphony.xwork2.ActionSupport; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; + +/** + * Example to illustrate the async plugin. + */ +public class ChatRoomAction extends ActionSupport { + private String message; + private Integer lastIndex; + private List newMessages; + + private static final List messages = new ArrayList<>(); + + public void setMessage(String message) { + this.message = message; + } + + public void setLastIndex(Integer lastIndex) { + this.lastIndex = lastIndex; + } + + public List getNewMessages() { + return newMessages; + } + + public Callable receiveNewMessages() throws Exception { + return new Callable() { + @Override + public String call() throws Exception { + while (lastIndex >= messages.size()) { + Thread.sleep(3000); + } + newMessages = messages.subList(lastIndex, messages.size()); + return SUCCESS; + } + }; + } + + public String sendMessage() { + synchronized (messages) { + messages.add(message); + } + return SUCCESS; + } +} diff --git a/apps/showcase/src/main/resources/struts-async.xml b/apps/showcase/src/main/resources/struts-async.xml new file mode 100644 index 000000000..faa38656c --- /dev/null +++ b/apps/showcase/src/main/resources/struts-async.xml @@ -0,0 +1,49 @@ + + + + + + + + + + newMessages + + + newMessages + + + + + + newMessages + + + newMessages + + + + + + diff --git a/apps/showcase/src/main/resources/struts.xml b/apps/showcase/src/main/resources/struts.xml index 46611f9cd..ee7dbcee3 100644 --- a/apps/showcase/src/main/resources/struts.xml +++ b/apps/showcase/src/main/resources/struts.xml @@ -74,6 +74,8 @@ + + diff --git a/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp b/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp index 727b28c5b..bcbe36113 100644 --- a/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp +++ b/apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp @@ -235,6 +235,7 @@
  • Token
  • Model Driven
  • +
  • Async