View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.example.demo;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.enterprise.context.RequestScoped;
26  import javax.faces.context.ExternalContext;
27  import javax.faces.context.FacesContext;
28  import javax.inject.Named;
29  import java.io.File;
30  import java.io.Serializable;
31  import java.io.UnsupportedEncodingException;
32  import java.lang.invoke.MethodHandles;
33  import java.net.MalformedURLException;
34  import java.net.URLEncoder;
35  import java.nio.charset.StandardCharsets;
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  @RequestScoped
40  @Named
41  public class TestController implements Serializable {
42  
43    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
44  
45    public boolean hasTest() {
46      final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
47      final String testJsUrl = "/" + getBase() + ".test.js";
48      try {
49        return externalContext.getResource(testJsUrl) != null;
50      } catch (final MalformedURLException e) {
51        LOG.error("URL was: " + testJsUrl, e);
52      }
53      return false;
54    }
55  
56    public String getBase() {
57      final FacesContext facesContext = FacesContext.getCurrentInstance();
58      final String viewId = facesContext.getViewRoot().getViewId();
59      return viewId.substring(1, viewId.length() - 6); //remove leading '/' and trailing '.xhtml'
60    }
61  
62    public List<TestPage> getAllTestPages() throws UnsupportedEncodingException {
63      final List<TestPage> pages = new ArrayList<>();
64  
65      String realPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
66      realPath = realPath.endsWith("/") ? realPath : realPath.concat("/");
67  
68      final File rootDir = new File(realPath + "content");
69      if (rootDir.exists()) {
70        int idCount = 1;
71        for (final String page : getXHTMLs(rootDir)) {
72          final String base = page.substring(realPath.length(), page.length() - ".xhtml".length());
73          pages.add(new TestPage("tp" + idCount++, URLEncoder.encode(base, StandardCharsets.UTF_8.name())));
74          // todo: StandardCharsets.UTF_8.name() can be simplified with Java 10
75        }
76      }
77      return pages;
78    }
79  
80    private List<String> getXHTMLs(final File dir) {
81      final List<String> xhtmls = new ArrayList<>();
82      for (final File file : dir.listFiles()) {
83        if (file.isDirectory()) {
84          xhtmls.addAll(getXHTMLs(file));
85        } else if (!file.getName().startsWith("x-") && file.getName().endsWith(".xhtml")) {
86          xhtmls.add(file.getPath());
87        }
88      }
89      return xhtmls;
90    }
91  
92    public List<TestPage> getTestPages() throws UnsupportedEncodingException {
93      final List<TestPage> testPages = new ArrayList<>();
94  
95      String realPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
96      realPath = realPath.endsWith("/") ? realPath : realPath.concat("/");
97  
98      final File rootDir = new File(realPath + "content");
99      if (rootDir.exists()) {
100       int idCount = 1;
101       for (final String testJs : getTestJs(rootDir)) {
102         final String base = testJs.substring(realPath.length(), testJs.length() - ".test.js".length());
103         testPages.add(new TestPage("tp" + idCount++, URLEncoder.encode(base, StandardCharsets.UTF_8.name())));
104         // todo: StandardCharsets.UTF_8.name() can be simplified with Java 10
105       }
106     }
107     return testPages;
108   }
109 
110   private List<String> getTestJs(final File dir) {
111     final List<String> testJsFiles = new ArrayList<>();
112     for (final File file : dir.listFiles()) {
113       if (file.isDirectory()) {
114         testJsFiles.addAll(getTestJs(file));
115       } else if (!file.getName().startsWith("x-") && file.getName().endsWith(".test.js")) {
116         testJsFiles.add(file.getPath());
117       }
118     }
119     return testJsFiles;
120   }
121 
122   public class TestPage {
123     private final String id;
124     private final String base;
125 
126     TestPage(final String id, final String base) {
127       this.id = id;
128       this.base = base;
129     }
130 
131     public String getId() {
132       return id;
133     }
134 
135     public String getBase() {
136       return base;
137     }
138   }
139 }