Search in sources :

Example 51 with RuntimeEnvironment

use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class Results method prettyPrint.

/**
 * Prints out results in html form. The following search helper fields are
 * required to be properly initialized: <ul>
 * <li>{@link SearchHelper#dataRoot}</li>
 * <li>{@link SearchHelper#contextPath}</li>
 * <li>{@link SearchHelper#searcher}</li> <li>{@link SearchHelper#hits}</li>
 * <li>{@link SearchHelper#historyContext} (ignored if {@code null})</li>
 * <li>{@link SearchHelper#sourceContext} (ignored if {@code null})</li>
 * <li>{@link SearchHelper#summarizer} (if sourceContext is not
 * {@code null})</li> <li>{@link SearchHelper#sourceRoot} (if
 * sourceContext or historyContext is not {@code null})</li> </ul>
 *
 * @param out write destination
 * @param sh search helper which has all required fields set
 * @param start index of the first hit to print
 * @param end index of the last hit to print
 * @throws HistoryException history exception
 * @throws IOException I/O exception
 * @throws ClassNotFoundException class not found
 */
public static void prettyPrint(Writer out, SearchHelper sh, int start, long end) throws HistoryException, IOException, ClassNotFoundException {
    Project p;
    String contextPath = sh.getContextPath();
    String ctxE = Util.uriEncodePath(contextPath);
    String xrefPrefix = contextPath + Prefix.XREF_P;
    String morePrefix = contextPath + Prefix.MORE_P;
    String xrefPrefixE = ctxE + Prefix.XREF_P;
    File xrefDataDir = new File(sh.getDataRoot(), Prefix.XREF_P.toString());
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    boolean evenRow = true;
    out.write("<tbody class=\"search-result\">");
    for (Map.Entry<String, ArrayList<Integer>> entry : createMap(sh.getSearcher(), sh.getHits(), start, end).entrySet()) {
        String parent = entry.getKey();
        out.write("<tr class=\"dir\"><td colspan=\"3\"><a href=\"");
        out.write(xrefPrefixE);
        out.write(Util.uriEncodePath(parent));
        out.write("/\">");
        out.write(htmlize(parent));
        out.write("/</a>");
        if (sh.getDesc() != null) {
            out.write(" - <i>");
            out.write(sh.getDesc().get(parent));
            out.write("</i>");
        }
        p = Project.getProject(parent);
        String messages = MessagesUtils.messagesToJson(p, MESSAGES_MAIN_PAGE_TAG);
        if (p != null && !messages.isEmpty()) {
            out.write(" <a href=\"" + xrefPrefix + "/" + p.getName() + "\">");
            out.write("<span class=\"note-" + MessagesUtils.getMessageLevel(p.getName(), MESSAGES_MAIN_PAGE_TAG) + " important-note important-note-rounded\" data-messages='" + messages + "'>!</span>");
            out.write("</a>");
        }
        int tabSize = sh.getTabSize(p);
        PrintPlainFinalArgs fargs = new PrintPlainFinalArgs(out, sh, env, xrefPrefix, tabSize, morePrefix);
        out.write("</td></tr>");
        for (int docId : entry.getValue()) {
            Document doc = sh.getSearcher().doc(docId);
            String rpath = doc.get(QueryBuilder.PATH);
            String rpathE = Util.uriEncodePath(rpath);
            if (evenRow) {
                out.write("<tr class=\"search-result-even-row\">");
            } else {
                out.write("<tr>");
            }
            evenRow = !evenRow;
            Util.writeHAD(out, sh.getContextPath(), rpathE, false);
            out.write("<td class=\"f\"><a href=\"");
            out.write(xrefPrefixE);
            out.write(rpathE);
            out.write("\"");
            if (env.isLastEditedDisplayMode()) {
                printLastEditedDate(out, doc);
            }
            out.write(">");
            out.write(htmlize(rpath.substring(rpath.lastIndexOf('/') + 1)));
            out.write("</a>");
            out.write("</td><td><code class=\"con\">");
            if (sh.getSourceContext() != null) {
                AbstractAnalyzer.Genre genre = AbstractAnalyzer.Genre.get(doc.get(QueryBuilder.T));
                Summarizer summarizer = sh.getSummarizer();
                if (AbstractAnalyzer.Genre.XREFABLE == genre && summarizer != null) {
                    String xtags = getTags(xrefDataDir, rpath, env.isCompressXref());
                    // FIXME use Highlighter from lucene contrib here,
                    // instead of summarizer, we'd also get rid of
                    // apache lucene in whole source ...
                    out.write(summarizer.getSummary(xtags).toString());
                } else if (AbstractAnalyzer.Genre.HTML == genre && summarizer != null) {
                    String htags = getTags(sh.getSourceRoot(), rpath, false);
                    out.write(summarizer.getSummary(htags).toString());
                } else if (genre == AbstractAnalyzer.Genre.PLAIN) {
                    printPlain(fargs, doc, docId, rpath);
                }
            }
            HistoryContext historyContext = sh.getHistoryContext();
            if (historyContext != null) {
                historyContext.getContext(new File(sh.getSourceRoot(), rpath), rpath, out, sh.getContextPath());
            }
            out.write("</code></td></tr>\n");
        }
    }
    out.write("</tbody>");
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) HistoryContext(org.opengrok.indexer.search.context.HistoryContext) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) Project(org.opengrok.indexer.configuration.Project) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 52 with RuntimeEnvironment

use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class AuthorizationEntityTest method testForGroupsAndForProjectsDiscovery.

@ParameterizedTest
@MethodSource("parameters")
public void testForGroupsAndForProjectsDiscovery(Function<Void, AuthorizationEntity> authEntityFactory) {
    Group g1, g2, g3;
    AuthorizationEntity authEntity;
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    env.setProjectsEnabled(true);
    env.getProjects().put("project 1", new Project("project 1"));
    env.getProjects().put("project 2", new Project("project 2"));
    env.getProjects().put("project 3", new Project("project 3"));
    env.getProjects().put("project 4", new Project("project 4"));
    env.getProjects().put("project 5", new Project("project 5"));
    env.getProjects().put("project 6", new Project("project 6"));
    env.getProjects().put("project 7", new Project("project 7"));
    env.getProjects().put("project 8", new Project("project 8"));
    env.getProjects().put("project 9", new Project("project 9"));
    /**
     * Structure<br>
     * <pre>
     * G1 + P1
     *    + P2
     *    + P3
     *    + G2
     *       + P4
     *       + P5
     *       + P6
     *       + P7
     * G3 + P8
     *    + P9
     * </pre>
     */
    g1 = new Group();
    g1.setName("group 1");
    g1.addProject(env.getProjects().get("project 1"));
    g1.addProject(env.getProjects().get("project 2"));
    g1.addProject(env.getProjects().get("project 3"));
    env.getGroups().add(g1);
    g2 = new Group();
    g2.setName("group 2");
    g2.addProject(env.getProjects().get("project 4"));
    g2.addProject(env.getProjects().get("project 5"));
    g2.addProject(env.getProjects().get("project 6"));
    g2.addProject(env.getProjects().get("project 7"));
    g1.addGroup(g2);
    env.getGroups().add(g2);
    g3 = new Group();
    g3.setName("group 3");
    g3.addProject(env.getProjects().get("project 8"));
    g3.addProject(env.getProjects().get("project 9"));
    env.getGroups().add(g3);
    // add g1 and all descendants their projects
    authEntity = authEntityFactory.apply(null);
    authEntity.setForGroups(new TreeSet<>());
    authEntity.setForGroups("group 1");
    authEntity.load(new TreeMap<>());
    assertEquals(new TreeSet<>(Arrays.asList("group 1", "group 2")), authEntity.forGroups());
    assertEquals(new TreeSet<>(Arrays.asList("project 1", "project 2", "project 3", "project 4", "project 5", "project 6", "project 7")), authEntity.forProjects());
    // add group2, its parent g1 and g2 projects
    authEntity = authEntityFactory.apply(null);
    authEntity.setForGroups(new TreeSet<>());
    authEntity.setForGroups("group 2");
    authEntity.load(new TreeMap<>());
    assertEquals(new TreeSet<>(Arrays.asList("group 1", "group 2")), authEntity.forGroups());
    assertEquals(new TreeSet<>(Arrays.asList("project 4", "project 5", "project 6", "project 7")), authEntity.forProjects());
    // add only g3 and its projects
    authEntity = authEntityFactory.apply(null);
    authEntity.setForGroups(new TreeSet<>());
    authEntity.setForGroups("group 3");
    authEntity.load(new TreeMap<>());
    assertEquals(new TreeSet<>(Collections.singletonList("group 3")), authEntity.forGroups());
    assertEquals(new TreeSet<>(Arrays.asList("project 8", "project 9")), authEntity.forProjects());
}
Also used : Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 53 with RuntimeEnvironment

use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class AuthorizationEntityTest method setUp.

@BeforeEach
public void setUp() {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    envGroups = env.getGroups();
    envProjects = env.getProjects();
    env.setGroups(new TreeSet<>());
    env.setProjects(new TreeMap<>());
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 54 with RuntimeEnvironment

use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class AuthorizationEntityTest method testForGroupsAndForProjectsDiscoveryInvalidProjectInGroup.

/**
 * Listed projects in the group don't exist.
 */
@ParameterizedTest
@MethodSource("parameters")
public void testForGroupsAndForProjectsDiscoveryInvalidProjectInGroup(Function<Void, AuthorizationEntity> authEntityFactory) {
    AuthorizationEntity authEntity = authEntityFactory.apply(null);
    authEntity.setForGroups(new TreeSet<>(Arrays.asList("group 1", "group 2")));
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    Group g1 = new Group();
    g1.setName("group 1");
    g1.addProject(new Project("project 1"));
    g1.addProject(new Project("project 2"));
    g1.addProject(new Project("project 3"));
    env.getGroups().add(g1);
    authEntity.load(new TreeMap<>());
    assertEquals(new TreeSet<>(Collections.singletonList("group 1")), authEntity.forGroups());
    assertEquals(new TreeSet<>(), authEntity.forProjects());
}
Also used : Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 55 with RuntimeEnvironment

use of org.opengrok.indexer.configuration.RuntimeEnvironment in project OpenGrok by OpenGrok.

the class MessagesUtils method getMessageLevel.

/**
 * @param tags message tags
 * @return name of highest cssClass of messages present in the system or null.
 */
public static String getMessageLevel(String... tags) {
    Set<MessagesContainer.AcceptedMessage> messages;
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    messages = Arrays.stream(tags).map(env::getMessages).flatMap(Collection::stream).collect(Collectors.toSet());
    return getHighestMessageLevel(messages);
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) Collection(java.util.Collection)

Aggregations

RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)81 File (java.io.File)26 Project (org.opengrok.indexer.configuration.Project)24 Test (org.junit.jupiter.api.Test)22 IOException (java.io.IOException)18 BeforeAll (org.junit.jupiter.api.BeforeAll)13 ArrayList (java.util.ArrayList)12 TestRepository (org.opengrok.indexer.util.TestRepository)12 Path (java.nio.file.Path)8 ForbiddenSymlinkException (org.opengrok.indexer.util.ForbiddenSymlinkException)8 Document (org.apache.lucene.document.Document)6 Ctags (org.opengrok.indexer.analysis.Ctags)6 Executor (org.opengrok.indexer.util.Executor)6 BufferedReader (java.io.BufferedReader)5 FileNotFoundException (java.io.FileNotFoundException)5 InputStreamReader (java.io.InputStreamReader)5 EnabledForRepository (org.opengrok.indexer.condition.EnabledForRepository)5 HistoryGuru (org.opengrok.indexer.history.HistoryGuru)5 BeforeEach (org.junit.jupiter.api.BeforeEach)4 RepositoryInfo (org.opengrok.indexer.history.RepositoryInfo)4