You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2022/06/18 06:12:36 UTC
[struts] 02/04: Fixes showing configuration of an action in Showcase app
This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a commit to branch WW-5190-match-action-proxy
in repository https://gitbox.apache.org/repos/asf/struts.git
commit 06290cef91779764c35f133d00632cd138cd25c4
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Sat Jun 18 07:26:13 2022 +0200
Fixes showing configuration of an action in Showcase app
---
.../struts2/showcase/source/ViewSourceAction.java | 385 ++++++++++-----------
.../src/main/webapp/WEB-INF/viewSource.jsp | 75 ++--
2 files changed, 222 insertions(+), 238 deletions(-)
diff --git a/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java b/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java
index 541adccff..ce5b5b949 100644
--- a/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java
+++ b/apps/showcase/src/main/java/org/apache/struts2/showcase/source/ViewSourceAction.java
@@ -22,16 +22,13 @@ package org.apache.struts2.showcase.source;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ClassLoaderUtil;
-import org.apache.struts2.ServletActionContext;
import org.apache.struts2.action.ServletContextAware;
import javax.servlet.ServletContext;
import java.io.BufferedReader;
-import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@@ -42,202 +39,192 @@ import java.util.List;
*/
public class ViewSourceAction extends ActionSupport implements ServletContextAware {
- private String page;
- private String className;
- private String config;
-
- private List pageLines;
- private List classLines;
- private List configLines;
-
- private int configLine;
- private int padding = 10;
-
- private ServletContext servletContext;
-
- public String execute() throws MalformedURLException, IOException {
-
- if (page != null) {
-
- InputStream in = ClassLoaderUtil.getResourceAsStream(page.substring(page.indexOf("//") + 1), getClass());
- page = page.replace("//", "/");
-
- if (in == null) {
- in = servletContext.getResourceAsStream(page);
- while (in == null && page.indexOf('/', 1) > 0) {
- page = page.substring(page.indexOf('/', 1));
- in = servletContext.getResourceAsStream(page);
- }
- }
- pageLines = read(in, -1);
-
- if (in != null) {
- in.close();
- }
- }
-
- if (className != null) {
- className = "/" + className.replace('.', '/') + ".java";
- InputStream in = getClass().getResourceAsStream(className);
- if (in == null) {
- in = servletContext.getResourceAsStream("/WEB-INF/src" + className);
- }
- classLines = read(in, -1);
-
- if (in != null) {
- in.close();
- }
- }
-
- final String rootPath = ServletActionContext.getServletContext().getRealPath("/");
- final String rootPathUnix = (rootPath != null ? rootPath.replace(File.separator, "/") : null); // Make path Unix-like for comparison (e.g. on Windows)
- final String rootPathFileURI = "file://" + rootPathUnix;
- final String collapsedRootPathFileURI = rootPathFileURI.replace("//", "/"); // Config string may have been transformed
- final String rootPathWarFileURI = "war:file://" + rootPathUnix;
- final String collapsedRootPathWarFileURI = rootPathWarFileURI.replace("//", "/"); // Config string may have been transformed
-
- if (config != null && (rootPath == null || config.startsWith(rootPath) ||
- config.startsWith(rootPathFileURI) || config.startsWith(collapsedRootPathFileURI) ||
- config.startsWith(rootPathWarFileURI) || config.startsWith(collapsedRootPathWarFileURI))) {
- int pos = config.lastIndexOf(':');
- configLine = Integer.parseInt(config.substring(pos + 1));
- config = config.substring(0, pos).replace("//", "/");
- configLines = read(new URL(config).openStream(), configLine);
- }
- return SUCCESS;
- }
-
-
- /**
- * @param className the className to set
- */
- public void setClassName(String className) {
- if (className != null && className.trim().length() > 0) {
- this.className = className;
- }
- }
-
- /**
- * @param config the config to set
- */
- public void setConfig(String config) {
- if (config != null && config.trim().length() > 0) {
- this.config = config;
- }
- }
-
- /**
- * @param page the page to set
- */
- public void setPage(String page) {
- if (page != null && page.trim().length() > 0) {
- this.page = page;
- }
- }
-
- /**
- * @param padding the padding to set
- */
- public void setPadding(int padding) {
- this.padding = padding;
- }
-
-
- /**
- * @return the classLines
- */
- public List getClassLines() {
- return classLines;
- }
-
- /**
- * @return the configLines
- */
- public List getConfigLines() {
- return configLines;
- }
-
- /**
- * @return the pageLines
- */
- public List getPageLines() {
- return pageLines;
- }
-
- /**
- * @return the className
- */
- public String getClassName() {
- return className;
- }
-
- /**
- * @return the config
- */
- public String getConfig() {
- return config;
- }
-
- /**
- * @return the page
- */
- public String getPage() {
- return page;
- }
-
- /**
- * @return the configLine
- */
- public int getConfigLine() {
- return configLine;
- }
-
- /**
- * @return the padding
- */
- public int getPadding() {
- return padding;
- }
-
- /**
- * Reads in a stream, optionally only including the target line number
- * and its padding
- *
- * @param in The input stream
- * @param targetLineNumber The target line number, negative to read all
- * @return A list of lines
- */
- private List read(InputStream in, int targetLineNumber) {
- List snippet = null;
- if (in != null) {
- snippet = new ArrayList();
- int startLine = 0;
- int endLine = Integer.MAX_VALUE;
- if (targetLineNumber > 0) {
- startLine = targetLineNumber - padding;
- endLine = targetLineNumber + padding;
- }
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
-
- int lineno = 0;
- String line;
- while ((line = reader.readLine()) != null) {
- lineno++;
- if (lineno >= startLine && lineno <= endLine) {
- snippet.add(line);
- }
- }
- } catch (Exception ex) {
- // ignoring as snippet not available isn't a big deal
- }
- }
- return snippet;
- }
-
- public void withServletContext(ServletContext arg0) {
- this.servletContext = arg0;
- }
+ private String page;
+ private String className;
+ private String config;
+
+ private List<String> pageLines;
+ private List<String> classLines;
+ private List<String> configLines;
+
+ private int configLine;
+ private int padding = 10;
+
+ private ServletContext servletContext;
+
+ public String execute() throws IOException {
+
+ if (page != null) {
+
+ InputStream in = ClassLoaderUtil.getResourceAsStream(page.substring(page.indexOf("//") + 1), getClass());
+ page = page.replace("//", "/");
+
+ if (in == null) {
+ in = servletContext.getResourceAsStream(page);
+ while (in == null && page.indexOf('/', 1) > 0) {
+ page = page.substring(page.indexOf('/', 1));
+ in = servletContext.getResourceAsStream(page);
+ }
+ }
+ pageLines = read(in, -1);
+
+ if (in != null) {
+ in.close();
+ }
+ }
+
+ if (className != null) {
+ className = "/" + className.replace('.', '/') + ".java";
+ InputStream in = getClass().getResourceAsStream(className);
+ if (in == null) {
+ in = servletContext.getResourceAsStream("/WEB-INF/src/java" + className);
+ }
+ classLines = read(in, -1);
+
+ if (in != null) {
+ in.close();
+ }
+ }
+
+ if (config != null && config.startsWith("file:/")) {
+ int pos = config.lastIndexOf(':');
+ configLine = Integer.parseInt(config.substring(pos + 1));
+ configLines = read(new URL(config.substring(0, pos)).openStream(), configLine);
+ }
+ return SUCCESS;
+ }
+
+
+ /**
+ * @param className the className to set
+ */
+ public void setClassName(String className) {
+ if (className != null && className.trim().length() > 0) {
+ this.className = className;
+ }
+ }
+
+ /**
+ * @param config the config to set
+ */
+ public void setConfig(String config) {
+ if (config != null && config.trim().length() > 0) {
+ this.config = config;
+ }
+ }
+
+ /**
+ * @param page the page to set
+ */
+ public void setPage(String page) {
+ if (page != null && page.trim().length() > 0) {
+ this.page = page;
+ }
+ }
+
+ /**
+ * @param padding the padding to set
+ */
+ public void setPadding(int padding) {
+ this.padding = padding;
+ }
+
+
+ /**
+ * @return the classLines
+ */
+ public List<String> getClassLines() {
+ return classLines;
+ }
+
+ /**
+ * @return the configLines
+ */
+ public List<String> getConfigLines() {
+ return configLines;
+ }
+
+ /**
+ * @return the pageLines
+ */
+ public List<String> getPageLines() {
+ return pageLines;
+ }
+
+ /**
+ * @return the className
+ */
+ public String getClassName() {
+ return className;
+ }
+
+ /**
+ * @return the config
+ */
+ public String getConfig() {
+ return config;
+ }
+
+ /**
+ * @return the page
+ */
+ public String getPage() {
+ return page;
+ }
+
+ /**
+ * @return the configLine
+ */
+ public int getConfigLine() {
+ return configLine;
+ }
+
+ /**
+ * @return the padding
+ */
+ public int getPadding() {
+ return padding;
+ }
+
+ /**
+ * Reads in a stream, optionally only including the target line number
+ * and its padding
+ *
+ * @param in The input stream
+ * @param targetLineNumber The target line number, negative to read all
+ * @return A list of lines
+ */
+ private List<String> read(InputStream in, int targetLineNumber) {
+ List<String> snippet = null;
+ if (in != null) {
+ snippet = new ArrayList<>();
+ int startLine = 0;
+ int endLine = Integer.MAX_VALUE;
+ if (targetLineNumber > 0) {
+ startLine = targetLineNumber - padding;
+ endLine = targetLineNumber + padding;
+ }
+ try {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+
+ int lineno = 0;
+ String line;
+ while ((line = reader.readLine()) != null) {
+ lineno++;
+ if (lineno >= startLine && lineno <= endLine) {
+ snippet.add(line);
+ }
+ }
+ } catch (Exception ex) {
+ // ignoring as snippet not available isn't a big deal
+ }
+ }
+ return snippet;
+ }
+
+ public void withServletContext(ServletContext arg0) {
+ this.servletContext = arg0;
+ }
}
diff --git a/apps/showcase/src/main/webapp/WEB-INF/viewSource.jsp b/apps/showcase/src/main/webapp/WEB-INF/viewSource.jsp
index 1255d2a3b..61bb6fc87 100644
--- a/apps/showcase/src/main/webapp/WEB-INF/viewSource.jsp
+++ b/apps/showcase/src/main/webapp/WEB-INF/viewSource.jsp
@@ -1,19 +1,19 @@
<!--
/*
* Licensed to the Apache Software Foundation (ASF) under one
-* or more contributor license agreements. See the NOTICE file
+* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
-* regarding copyright ownership. The ASF licenses this file
+* 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
+* with the License. You may obtain a copy of the License at
*
-* http://www.apache.org/licenses/LICENSE-2.0
+* 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
+* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
@@ -26,49 +26,46 @@
<body>
<div class="container-fluid">
- <div class="row">
- <div class="col-md-12">
- <h1>View Sources</h1>
+ <div class="row">
+ <div class="col-md-12">
+ <h1>View Sources</h1>
- <ul class="nav nav-tabs" id="codeTab">
- <li class="active"><a href="#page">Page</a></li>
- <li><a href="#config">Configuration</a></li>
- <li><a href="#java">Java Action</a></li>
- </ul>
+ <ul class="nav nav-tabs" id="codeTab">
+ <li class="active"><a href="#page">Page</a></li>
+ <li><a href="#config">Configuration</a></li>
+ <li><a href="#java">Java Action</a></li>
+ </ul>
- <div class="tab-content">
- <div class="tab-pane active" id="page">
- <h3><s:property default="Unknown page" value="page"/></h3>
- <pre class="prettyprint lang-html linenums">
- <s:iterator value="pageLines" status="row">
+ <div class="tab-content">
+ <div class="tab-pane active" id="page">
+ <h3><s:property default="Unknown page" value="page"/></h3>
+ <pre class="prettyprint lang-html linenums"><s:iterator value="pageLines" status="row">
<s:property/></s:iterator>
- </pre>
- </div>
- <div class="tab-pane" id="config">
- <h3><s:property default="Unknown configuration" value="config"/></h3>
- <pre class="prettyprint lang-xml linenums">
- <s:iterator value="configLines" status="row">
+ </pre>
+ </div>
+ <div class="tab-pane" id="config">
+ <h3><s:property default="Unknown configuration" value="config"/></h3>
+ <pre class="prettyprint lang-xml linenums"><s:iterator value="configLines" status="row">
<s:property/></s:iterator>
- </pre>
- </div>
- <div class="tab-pane" id="java">
- <h3><s:property default="Unknown or unavailable Action class" value="className"/></h3>
- <pre class="prettyprint lang-java linenums">
- <s:iterator value="classLines" status="row">
+ </pre>
+ </div>
+ <div class="tab-pane" id="java">
+ <h3><s:property default="Unknown or unavailable Action class" value="className"/></h3>
+ <pre class="prettyprint lang-java linenums"><s:iterator value="classLines" status="row">
<s:property/></s:iterator>
- </pre>
- </div>
- </div>
- </div>
- </div>
+ </pre>
+ </div>
+ </div>
+ </div>
+ </div>
</div>
<s:script>
- $('#codeTab a').click(function (e) {
- e.preventDefault();
- $(this).tab('show');
- })
+ $('#codeTab a').click(function (e) {
+ e.preventDefault();
+ $(this).tab('show');
+ })
</s:script>
</body>
</html>