Search in sources :

Example 6 with Project

use of org.opensolaris.opengrok.configuration.Project in project OpenGrok by OpenGrok.

the class AuthorizationFilter method doFilter.

@Override
public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException {
    HttpServletRequest httpReq = (HttpServletRequest) sr;
    HttpServletResponse httpRes = (HttpServletResponse) sr1;
    PageConfig config = PageConfig.get(httpReq);
    long processTime = System.currentTimeMillis();
    Project p = config.getProject();
    if (p != null && !config.isAllowed(p)) {
        LOGGER.log(Level.INFO, "access denied for uri: {0}", httpReq.getRequestURI());
        config.getEnv().getStatistics().addRequest(httpReq);
        config.getEnv().getStatistics().addRequest(httpReq, "requests_forbidden");
        config.getEnv().getStatistics().addRequestTime(httpReq, "requests_forbidden", System.currentTimeMillis() - processTime);
        httpRes.sendError(403, "Access forbidden");
        return;
    }
    fc.doFilter(sr, sr1);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Project(org.opensolaris.opengrok.configuration.Project) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 7 with Project

use of org.opensolaris.opengrok.configuration.Project 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#compressed} (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
     * @throws IOException
     * @throws ClassNotFoundException
     */
public static void prettyPrint(Writer out, SearchHelper sh, int start, int end) throws HistoryException, IOException, ClassNotFoundException {
    Project p;
    String ctxE = Util.URIEncodePath(sh.contextPath);
    String xrefPrefix = sh.contextPath + Prefix.XREF_P;
    String morePrefix = sh.contextPath + Prefix.MORE_P;
    String xrefPrefixE = ctxE + Prefix.XREF_P;
    File xrefDataDir = new File(sh.dataRoot, Prefix.XREF_P.toString());
    for (Map.Entry<String, ArrayList<Document>> entry : createMap(sh.searcher, sh.hits, 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("/\">");
        // htmlize ???
        out.write(parent);
        out.write("/</a>");
        if (sh.desc != null) {
            out.write(" - <i>");
            // htmlize ???
            out.write(sh.desc.get(parent));
            out.write("</i>");
        }
        JSONArray messages;
        if ((p = Project.getProject(parent)) != null && (messages = Util.messagesToJson(p, RuntimeEnvironment.MESSAGES_MAIN_PAGE_TAG)).size() > 0) {
            out.write(" <a ");
            out.write("href=\"" + xrefPrefix + "/" + p.getName() + "\">");
            out.write("<span class=\"important-note important-note-rounded\" data-messages='" + messages + "'>!</span>");
            out.write("</a>");
        }
        out.write("</td></tr>");
        for (Document doc : entry.getValue()) {
            String rpath = doc.get(QueryBuilder.PATH);
            String rpathE = Util.URIEncodePath(rpath);
            DateFormat df;
            out.write("<tr>");
            Util.writeHAD(out, sh.contextPath, rpathE, false);
            out.write("<td class=\"f\"><a href=\"");
            out.write(xrefPrefixE);
            out.write(rpathE);
            out.write("\"");
            if (RuntimeEnvironment.getInstance().isLastEditedDisplayMode()) {
                try {
                    // insert last edited date if possible
                    df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
                    String dd = df.format(DateTools.stringToDate(doc.get("date")));
                    out.write(" class=\"result-annotate\" title=\"");
                    out.write("Last modified: ");
                    out.write(dd);
                    out.write("\"");
                } catch (ParseException ex) {
                    LOGGER.log(Level.WARNING, "An error parsing date information", ex);
                }
            }
            out.write(">");
            // htmlize ???
            out.write(rpath.substring(rpath.lastIndexOf('/') + 1));
            out.write("</a>");
            out.write("</td><td><tt class=\"con\">");
            if (sh.sourceContext != null) {
                Genre genre = Genre.get(doc.get("t"));
                Definitions tags = null;
                IndexableField tagsField = doc.getField(QueryBuilder.TAGS);
                if (tagsField != null) {
                    tags = Definitions.deserialize(tagsField.binaryValue().bytes);
                }
                Scopes scopes;
                IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
                if (scopesField != null) {
                    scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
                } else {
                    scopes = new Scopes();
                }
                if (Genre.XREFABLE == genre && sh.summarizer != null) {
                    String xtags = getTags(xrefDataDir, rpath, sh.compressed);
                    // FIXME use Highlighter from lucene contrib here,
                    // instead of summarizer, we'd also get rid of
                    // apache lucene in whole source ...
                    out.write(sh.summarizer.getSummary(xtags).toString());
                } else if (Genre.HTML == genre && sh.summarizer != null) {
                    String htags = getTags(sh.sourceRoot, rpath, false);
                    out.write(sh.summarizer.getSummary(htags).toString());
                } else {
                    FileReader r = genre == Genre.PLAIN ? new FileReader(new File(sh.sourceRoot, rpath)) : null;
                    sh.sourceContext.getContext(r, out, xrefPrefix, morePrefix, rpath, tags, true, sh.builder.isDefSearch(), null, scopes);
                }
            }
            if (sh.historyContext != null) {
                sh.historyContext.getContext(new File(sh.sourceRoot, rpath), rpath, out, sh.contextPath);
            }
            out.write("</tt></td></tr>\n");
        }
    }
}
Also used : Definitions(org.opensolaris.opengrok.analysis.Definitions) ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) Document(org.apache.lucene.document.Document) IndexableField(org.apache.lucene.index.IndexableField) Project(org.opensolaris.opengrok.configuration.Project) Scopes(org.opensolaris.opengrok.analysis.Scopes) DateFormat(java.text.DateFormat) FileReader(java.io.FileReader) ParseException(java.text.ParseException) Genre(org.opensolaris.opengrok.analysis.FileAnalyzer.Genre) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with Project

use of org.opensolaris.opengrok.configuration.Project in project OpenGrok by OpenGrok.

the class AuthorizationFrameworkTest method createAllowedProject.

private Project createAllowedProject() {
    Project p = new Project();
    p.setName("allowed" + "_" + "project" + Math.random());
    return p;
}
Also used : Project(org.opensolaris.opengrok.configuration.Project)

Example 9 with Project

use of org.opensolaris.opengrok.configuration.Project in project OpenGrok by OpenGrok.

the class AuthorizationPluginClassLoaderTest method testTruePlugin.

@Test
public void testTruePlugin() {
    AuthorizationPluginClassLoader instance = new AuthorizationPluginClassLoader(pluginDirectory);
    Class clazz = loadClass(instance, "org.sample.plugin.TruePlugin");
    IAuthorizationPlugin plugin = getNewInstance(clazz);
    Group g = new Group();
    g.setName("group1");
    Project p = new Project();
    p.setName(("project1"));
    Assert.assertTrue(plugin.isAllowed(new DummyHttpServletRequest(), g));
    Assert.assertTrue(plugin.isAllowed(new DummyHttpServletRequest(), p));
}
Also used : Group(org.opensolaris.opengrok.configuration.Group) Project(org.opensolaris.opengrok.configuration.Project) DummyHttpServletRequest(org.opensolaris.opengrok.web.DummyHttpServletRequest) Test(org.junit.Test)

Example 10 with Project

use of org.opensolaris.opengrok.configuration.Project in project OpenGrok by OpenGrok.

the class AuthorizationPluginClassLoaderTest method testFalsePlugin.

@Test
public void testFalsePlugin() {
    AuthorizationPluginClassLoader instance = new AuthorizationPluginClassLoader(pluginDirectory);
    Class clazz = loadClass(instance, "org.sample.plugin.FalsePlugin");
    IAuthorizationPlugin plugin = getNewInstance(clazz);
    Group g = new Group();
    g.setName("group1");
    Project p = new Project();
    p.setName(("project1"));
    Assert.assertFalse(plugin.isAllowed(new DummyHttpServletRequest(), g));
    Assert.assertFalse(plugin.isAllowed(new DummyHttpServletRequest(), p));
}
Also used : Group(org.opensolaris.opengrok.configuration.Group) Project(org.opensolaris.opengrok.configuration.Project) DummyHttpServletRequest(org.opensolaris.opengrok.web.DummyHttpServletRequest) Test(org.junit.Test)

Aggregations

Project (org.opensolaris.opengrok.configuration.Project)44 Test (org.junit.Test)22 RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)12 Group (org.opensolaris.opengrok.configuration.Group)9 ArrayList (java.util.ArrayList)8 RepositoryInfo (org.opensolaris.opengrok.history.RepositoryInfo)6 File (java.io.File)4 TreeSet (java.util.TreeSet)4 IOException (java.io.IOException)3 HistoryException (org.opensolaris.opengrok.history.HistoryException)3 FileNotFoundException (java.io.FileNotFoundException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Document (org.apache.lucene.document.Document)2 Genre (org.opensolaris.opengrok.analysis.FileAnalyzer.Genre)2 RepoRepository (org.opensolaris.opengrok.history.RepoRepository)2 DummyHttpServletRequest (org.opensolaris.opengrok.web.DummyHttpServletRequest)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1