Search in sources :

Example 51 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 w.r.t. 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++) {
                // SRCROOT is read with UTF-8 as a default.
                try (BufferedReader br = new BufferedReader(ExpandTabsReader.wrap(IOUtils.createBOMStrippedReader(in[i], StandardCharsets.UTF_8.name()), 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) 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) HistoryException(org.opensolaris.opengrok.history.HistoryException) 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)

Example 52 with Project

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

the class PageConfig method getRequestedProjects.

/**
 * Same as {@link #getRequestedProjects()}, but with a variable cookieName
 * and parameter name. This way it is trivial to implement a project filter
 * ...
 *
 * @param paramName the name of the request parameter, which possibly
 * contains the project list in question.
 * @param cookieName name of the cookie which possible contains project
 * lists used as fallback
 * @return set of project names. Possibly empty set but never {@code null}.
 */
protected SortedSet<String> getRequestedProjects(String paramName, String cookieName) {
    TreeSet<String> set = new TreeSet<>();
    List<Project> projects = getEnv().getProjectList();
    if (projects == null) {
        return set;
    }
    /**
     * If the project was determined from the URL, use this project.
     */
    if (getProject() != null) {
        set.add(getProject().getName());
        return set;
    }
    if (projects.size() == 1) {
        Project p = projects.get(0);
        if (authFramework.isAllowed(req, p)) {
            set.add(p.getName());
        }
        return set;
    }
    List<String> vals = getParamVals(paramName);
    for (String s : vals) {
        Project x = Project.getByName(s);
        if (x != null && authFramework.isAllowed(req, x)) {
            set.add(s);
        }
    }
    if (set.isEmpty()) {
        List<String> cookies = getCookieVals(cookieName);
        for (String s : cookies) {
            Project x = Project.getByName(s);
            if (x != null && authFramework.isAllowed(req, x)) {
                set.add(s);
            }
        }
    }
    if (set.isEmpty()) {
        Set<Project> defaultProjects = env.getDefaultProjects();
        if (defaultProjects != null) {
            for (Project project : defaultProjects) {
                if (authFramework.isAllowed(req, project)) {
                    set.add(project.getName());
                }
            }
        }
    }
    return set;
}
Also used : Project(org.opensolaris.opengrok.configuration.Project) TreeSet(java.util.TreeSet)

Example 53 with Project

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

the class ProjectHelper method populateGroups.

/**
 * Generates ungrouped projects and repositories.
 */
private void populateGroups() {
    groups.addAll(cfg.getEnv().getGroups());
    for (Project project : cfg.getEnv().getProjectList()) {
        // filterProjects() only adds groups which match project's name.
        Set<Group> copy = Group.matching(project, groups);
        // If no group matches the project, add it to not-grouped projects.
        if (copy.isEmpty()) {
            if (cfg.getEnv().getProjectRepositoriesMap().get(project) == null) {
                ungroupedProjects.add(project);
            } else {
                ungroupedRepositories.add(project);
            }
        }
    }
    // populate all grouped
    for (Group g : getGroups()) {
        allProjects.addAll(g.getProjects());
        allRepositories.addAll(g.getRepositories());
    }
}
Also used : Project(org.opensolaris.opengrok.configuration.Project) Group(org.opensolaris.opengrok.configuration.Group)

Example 54 with Project

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

the class IndexDatabase method optimizeAll.

/**
 * Optimize all index databases
 *
 * @param parallelizer a defined instance
 * @throws IOException if an error occurs
 */
static void optimizeAll(IndexerParallelizer parallelizer) throws IOException {
    List<IndexDatabase> dbs = new ArrayList<>();
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (env.hasProjects()) {
        for (Project project : env.getProjectList()) {
            dbs.add(new IndexDatabase(project));
        }
    } else {
        dbs.add(new IndexDatabase());
    }
    for (IndexDatabase d : dbs) {
        final IndexDatabase db = d;
        if (db.isDirty()) {
            parallelizer.getFixedExecutor().submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        db.update(parallelizer);
                    } catch (Throwable e) {
                        LOGGER.log(Level.SEVERE, "Problem updating lucene index database: ", e);
                    }
                }
            });
        }
    }
}
Also used : Project(org.opensolaris.opengrok.configuration.Project) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList)

Example 55 with Project

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

the class AuthorizationEntity method processTargetGroupsAndProjects.

/**
 * Discover all targeted groups and projects for every group given by
 * {@link #forGroups()}.
 *
 * <ul>
 * <li>add to the {@link #forGroups()} all groups which are descendant
 * groups to the group</li>
 * <li>add to the {@link #forGroups()} all groups which are parent groups to
 * the group</li>
 * <li>add to the {@link #forProjects()} all projects and repositories which
 * are in the descendant groups or in the group itself</li>
 * <li>issue a warning for non-existent groups</li>
 * <li>issue a warning for non-existent projects</li>
 * </ul>
 */
protected void processTargetGroupsAndProjects() {
    Set<String> groups = new TreeSet<>();
    for (String x : forGroups()) {
        /**
         * Full group discovery takes place here. All projects/repositories
         * in the group are added into "forProjects" and all subgroups
         * (including projects/repositories) and parent groups (excluding
         * the projects/repositories) are added into "forGroups".
         *
         * If the group does not exist then a warning is issued.
         */
        Group g;
        if ((g = Group.getByName(x)) != null) {
            forProjects().addAll(g.getAllProjects().stream().map((t) -> t.getName()).collect(Collectors.toSet()));
            groups.addAll(g.getRelatedGroups().stream().map((t) -> t.getName()).collect(Collectors.toSet()));
            groups.add(x);
        } else {
            LOGGER.log(Level.WARNING, "Configured group \"{0}\" in forGroups section" + " for name \"{1}\" does not exist", new Object[] { x, getName() });
        }
    }
    setForGroups(groups);
    forProjects().removeIf((t) -> {
        /**
         * Check the existence of the projects and issue a warning if there
         * is no such project.
         */
        Project p;
        if ((p = Project.getByName(t)) == null) {
            LOGGER.log(Level.WARNING, "Configured project \"{0}\" in forProjects" + " section for name \"{1}\" does not exist", new Object[] { t, getName() });
            return true;
        }
        return false;
    });
}
Also used : Group(org.opensolaris.opengrok.configuration.Group) Project(org.opensolaris.opengrok.configuration.Project) TreeSet(java.util.TreeSet)

Aggregations

Project (org.opensolaris.opengrok.configuration.Project)79 Test (org.junit.Test)40 RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)31 File (java.io.File)20 ArrayList (java.util.ArrayList)20 Group (org.opensolaris.opengrok.configuration.Group)17 RepositoryInfo (org.opensolaris.opengrok.history.RepositoryInfo)14 IOException (java.io.IOException)12 TreeSet (java.util.TreeSet)12 HistoryException (org.opensolaris.opengrok.history.HistoryException)8 List (java.util.List)6 ParseException (java.text.ParseException)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 Repository (org.opensolaris.opengrok.history.Repository)5 TestRepository (org.opensolaris.opengrok.util.TestRepository)5 ConnectException (java.net.ConnectException)4