Search in sources :

Example 21 with Project

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

the class ProjectHelperTest method testGetAllGroupedAllowedGroup.

/**
     * Test of getAllGrouped method, of class ProjectHelper.
     */
@Test
public void testGetAllGroupedAllowedGroup() {
    for (Group g : RuntimeEnvironment.getInstance().getGroups()) {
        if (g.getName().startsWith("allowed_group_0")) {
            Set<Project> result = helper.getAllGrouped(g);
            Assert.assertEquals(4, result.size());
            for (Project p : result) {
                Assert.assertTrue(p.getName().startsWith("allowed_"));
            }
        }
    }
}
Also used : Group(org.opensolaris.opengrok.configuration.Group) Project(org.opensolaris.opengrok.configuration.Project) Test(org.junit.Test)

Example 22 with Project

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

the class ProjectHelperTest method testUnAllowedGetRepositoryInfo.

/**
     * Test of getRepositoryInfo method, of class ProjectHelper.
     */
@Test
public void testUnAllowedGetRepositoryInfo() {
    Project p = new Project();
    p.setName("repository_2_1");
    List<RepositoryInfo> result = helper.getRepositoryInfo(p);
    Assert.assertEquals("this project is not allowed", 0, result.size());
}
Also used : Project(org.opensolaris.opengrok.configuration.Project) RepositoryInfo(org.opensolaris.opengrok.history.RepositoryInfo) Test(org.junit.Test)

Example 23 with Project

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

the class ProjectHelperTestBase method setUp.

@Before
public void setUp() {
    Assert.assertEquals("Should contain 4 groups", 4, env.getGroups().size());
    Assert.assertEquals("Should contain 40 project", 40, env.getProjects().size());
    Assert.assertEquals("Should contain 20 repositories", 20, env.getRepositories().size());
    Assert.assertNotNull("Repository map should not be null", env.getProjectRepositoriesMap());
    Assert.assertEquals("Repository map should contain 20 project", 20, env.getProjectRepositoriesMap().size());
    invokeRemoveAll();
    instance = getInstance();
    IAuthorizationPlugin plugin = new TestPlugin() {

        @Override
        public boolean isAllowed(HttpServletRequest request, Project project) {
            return project.getName().startsWith("allowed");
        }

        @Override
        public boolean isAllowed(HttpServletRequest request, Group group) {
            return group.getName().startsWith("allowed");
        }
    };
    invokeAddPlugin(plugin);
    cfg = PageConfig.get(getRequest());
    helper = cfg.getProjectHelper();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Project(org.opensolaris.opengrok.configuration.Project) Group(org.opensolaris.opengrok.configuration.Group) IAuthorizationPlugin(org.opensolaris.opengrok.authorization.IAuthorizationPlugin) TestPlugin(org.opensolaris.opengrok.authorization.TestPlugin) Before(org.junit.Before)

Example 24 with Project

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

the class SearchEngine method searchMultiDatabase.

/**
     * Perform search on multiple indexes in parallel.
     * @param paging whether to use paging (if yes, first X pages will load
     * faster)
     * @param root list of projects to search
     * @throws IOException
     */
private void searchMultiDatabase(List<Project> root, boolean paging) throws IOException {
    SortedSet<String> projects = new TreeSet<>();
    for (Project p : root) {
        projects.add(p.getName());
    }
    // We use MultiReader even for single project. This should
    // not matter given that MultiReader is just a cheap wrapper
    // around set of IndexReader objects.
    MultiReader searchables = RuntimeEnvironment.getInstance().getMultiReader(projects, searcherList);
    searcher = new IndexSearcher(searchables);
    collector = TopScoreDocCollector.create(hitsPerPage * cachePages);
    searcher.search(query, collector);
    totalHits = collector.getTotalHits();
    if (!paging && totalHits > 0) {
        collector = TopScoreDocCollector.create(totalHits);
        searcher.search(query, collector);
    }
    hits = collector.topDocs().scoreDocs;
    for (ScoreDoc hit : hits) {
        int docId = hit.doc;
        Document d = searcher.doc(docId);
        docs.add(d);
    }
}
Also used : SuperIndexSearcher(org.opensolaris.opengrok.configuration.SuperIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) Project(org.opensolaris.opengrok.configuration.Project) TreeSet(java.util.TreeSet) MultiReader(org.apache.lucene.index.MultiReader) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 25 with Project

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

the class PageConfig method getDiffData.

/**
     * Get all data required to create a diff view wrt. to this request in one
     * go.
     *
     * @return an instance with just enough information to render a sufficient
     * view. If not all required parameters were given either they are
     * supplemented with reasonable defaults if possible, otherwise the related
     * field(s) are {@code null}. {@link DiffData#errorMsg}
     *  {@code != null} indicates, that an error occured and one should not try
     * to render a view.
     */
public DiffData getDiffData() {
    DiffData data = new DiffData();
    data.path = getPath().substring(0, path.lastIndexOf('/'));
    data.filename = Util.htmlize(getResourceFile().getName());
    String srcRoot = getSourceRootPath();
    String context = req.getContextPath();
    String[] filepath = new String[2];
    data.rev = new String[2];
    data.file = new String[2][];
    data.param = new String[2];
    /*
         * Basically the request URI looks like this:
         * http://$site/$webapp/diff/$resourceFile?r1=$fileA@$revA&r2=$fileB@$revB
         * The code below extracts file path and revision from the URI.
         */
    for (int i = 1; i <= 2; i++) {
        String p = req.getParameter("r" + i);
        if (p != null) {
            int j = p.lastIndexOf("@");
            if (j != -1) {
                filepath[i - 1] = p.substring(0, j);
                data.rev[i - 1] = p.substring(j + 1);
            }
        }
    }
    if (data.rev[0] == null || data.rev[1] == null || data.rev[0].length() == 0 || data.rev[1].length() == 0 || data.rev[0].equals(data.rev[1])) {
        data.errorMsg = "Please pick two revisions to compare the changed " + "from the <a href=\"" + context + Prefix.HIST_L + getUriEncodedPath() + "\">history</a>";
        return data;
    }
    data.genre = AnalyzerGuru.getGenre(getResourceFile().getName());
    if (data.genre == null || txtGenres.contains(data.genre)) {
        InputStream[] in = new InputStream[2];
        try {
            // Get input stream for both older and newer file.
            for (int i = 0; i < 2; i++) {
                File f = new File(srcRoot + filepath[i]);
                in[i] = HistoryGuru.getInstance().getRevision(f.getParent(), f.getName(), data.rev[i]);
                if (in[i] == null) {
                    data.errorMsg = "Unable to get revision " + Util.htmlize(data.rev[i]) + " for file: " + Util.htmlize(getPath());
                    return data;
                }
            }
            /*
                 * If the genre of the older revision cannot be determined,
                 * (this can happen if the file was empty), try with newer
                 * version.
                 */
            for (int i = 0; i < 2 && data.genre == null; i++) {
                try {
                    data.genre = AnalyzerGuru.getGenre(in[i]);
                } catch (IOException e) {
                    data.errorMsg = "Unable to determine the file type: " + Util.htmlize(e.getMessage());
                }
            }
            if (data.genre != Genre.PLAIN && data.genre != Genre.HTML) {
                return data;
            }
            ArrayList<String> lines = new ArrayList<>();
            Project p = getProject();
            for (int i = 0; i < 2; i++) {
                try (BufferedReader br = new BufferedReader(ExpandTabsReader.wrap(new InputStreamReader(in[i]), p))) {
                    String line;
                    while ((line = br.readLine()) != null) {
                        lines.add(line);
                    }
                    data.file[i] = lines.toArray(new String[lines.size()]);
                    lines.clear();
                }
                in[i] = null;
            }
        } catch (Exception e) {
            data.errorMsg = "Error reading revisions: " + Util.htmlize(e.getMessage());
        } finally {
            for (int i = 0; i < 2; i++) {
                IOUtils.close(in[i]);
            }
        }
        if (data.errorMsg != null) {
            return data;
        }
        try {
            data.revision = Diff.diff(data.file[0], data.file[1]);
        } catch (DifferentiationFailedException e) {
            data.errorMsg = "Unable to get diffs: " + Util.htmlize(e.getMessage());
        }
        for (int i = 0; i < 2; i++) {
            try {
                URI u = new URI(null, null, null, filepath[i] + "@" + data.rev[i], null);
                data.param[i] = u.getRawQuery();
            } catch (URISyntaxException e) {
                LOGGER.log(Level.WARNING, "Failed to create URI: ", e);
            }
        }
        data.full = fullDiff();
        data.type = getDiffType();
    }
    return data;
}
Also used : DifferentiationFailedException(org.apache.commons.jrcs.diff.DifferentiationFailedException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DifferentiationFailedException(org.apache.commons.jrcs.diff.DifferentiationFailedException) URISyntaxException(java.net.URISyntaxException) InvalidParameterException(java.security.InvalidParameterException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Project(org.opensolaris.opengrok.configuration.Project) BufferedReader(java.io.BufferedReader) File(java.io.File)

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