use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.
the class AnalyzerGuru method populateDocument.
/**
* Populate a Lucene document with the required fields.
*
* @param doc The document to populate
* @param file The file to index
* @param path Where the file is located (from source root)
* @param fa The analyzer to use on the file
* @param xrefOut Where to write the xref (possibly {@code null})
* @throws IOException If an exception occurs while collecting the data
* @throws InterruptedException if a timeout occurs
*/
public void populateDocument(Document doc, File file, String path, AbstractAnalyzer fa, Writer xrefOut) throws IOException, InterruptedException {
String date = DateTools.timeToString(file.lastModified(), DateTools.Resolution.MILLISECOND);
path = Util.fixPathIfWindows(path);
doc.add(new Field(QueryBuilder.U, Util.path2uid(path, date), string_ft_stored_nanalyzed_norms));
doc.add(new Field(QueryBuilder.FULLPATH, file.getAbsolutePath(), string_ft_nstored_nanalyzed_norms));
doc.add(new SortedDocValuesField(QueryBuilder.FULLPATH, new BytesRef(file.getAbsolutePath())));
if (RuntimeEnvironment.getInstance().isHistoryEnabled()) {
try {
HistoryGuru histGuru = HistoryGuru.getInstance();
HistoryReader hr = histGuru.getHistoryReader(file);
if (hr != null) {
doc.add(new TextField(QueryBuilder.HIST, hr));
History history;
if ((history = histGuru.getHistory(file)) != null) {
List<HistoryEntry> historyEntries = history.getHistoryEntries(1, 0);
if (!historyEntries.isEmpty()) {
HistoryEntry histEntry = historyEntries.get(0);
doc.add(new TextField(QueryBuilder.LASTREV, histEntry.getRevision(), Store.YES));
}
}
}
} catch (HistoryException e) {
LOGGER.log(Level.WARNING, "An error occurred while reading history: ", e);
}
}
doc.add(new Field(QueryBuilder.DATE, date, string_ft_stored_nanalyzed_norms));
doc.add(new SortedDocValuesField(QueryBuilder.DATE, new BytesRef(date)));
// `path' is not null, as it was passed to Util.path2uid() above.
doc.add(new TextField(QueryBuilder.PATH, path, Store.YES));
Project project = Project.getProject(path);
if (project != null) {
doc.add(new TextField(QueryBuilder.PROJECT, project.getPath(), Store.YES));
}
/*
* Use the parent of the path -- not the absolute file as is done for
* FULLPATH -- so that DIRPATH is the same convention as for PATH
* above. A StringField, however, is used instead of a TextField.
*/
File fpath = new File(path);
String fileParent = fpath.getParent();
if (fileParent != null && fileParent.length() > 0) {
String normalizedPath = QueryBuilder.normalizeDirPath(fileParent);
StringField npstring = new StringField(QueryBuilder.DIRPATH, normalizedPath, Store.NO);
doc.add(npstring);
}
if (fa != null) {
AbstractAnalyzer.Genre g = fa.getGenre();
if (g == AbstractAnalyzer.Genre.PLAIN || g == AbstractAnalyzer.Genre.XREFABLE || g == AbstractAnalyzer.Genre.HTML) {
doc.add(new Field(QueryBuilder.T, g.typeName(), string_ft_stored_nanalyzed_norms));
}
fa.analyze(doc, StreamSource.fromFile(file), xrefOut);
String type = fa.getFileTypeName();
doc.add(new StringField(QueryBuilder.TYPE, type, Store.YES));
}
}
use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.
the class AuthorizationFilter method doFilter.
/**
* All RESTful API requests are allowed here because they go through
* {@link org.opengrok.web.api.v1.filter.IncomingFilter}.
* The /search endpoint will go through authorization via SearchEngine.search()
* so does not have to be exempted here.
*/
@Override
public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException {
HttpServletRequest httpReq = (HttpServletRequest) sr;
HttpServletResponse httpRes = (HttpServletResponse) sr1;
if (httpReq.getServletPath().startsWith(RestApp.API_PATH)) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.log(Level.FINER, "Allowing request to {0} in {1}", new Object[] { Laundromat.launderLog(httpReq.getServletPath()), AuthorizationFilter.class.getName() });
}
fc.doFilter(sr, sr1);
return;
}
PageConfig config = PageConfig.get(httpReq);
Project p = config.getProject();
if (p != null && !config.isAllowed(p)) {
if (LOGGER.isLoggable(Level.INFO)) {
if (httpReq.getRemoteUser() != null) {
LOGGER.log(Level.INFO, "Access denied for user ''{0}'' for URI: {1}", new Object[] { Laundromat.launderLog(httpReq.getRemoteUser()), Laundromat.launderLog(httpReq.getRequestURI()) });
} else {
LOGGER.log(Level.INFO, "Access denied for URI: {0}", Laundromat.launderLog(httpReq.getRequestURI()));
}
}
if (!config.getEnv().getIncludeFiles().getForbiddenIncludeFileContent(false).isEmpty()) {
sr.getRequestDispatcher("/eforbidden").forward(sr, sr1);
return;
}
httpRes.sendError(HttpServletResponse.SC_FORBIDDEN, "Access forbidden");
return;
}
fc.doFilter(sr, sr1);
}
use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.
the class PageConfig method getRequestedProjects.
/**
* Same as {@link #getRequestedProjects()}, but with a variable cookieName
* and parameter name.
*
* @param searchAllParamName the name of the request parameter corresponding to search all projects.
* @param projectParamName the name of the request parameter corresponding to a project name.
* @param groupParamName the name of the request parameter corresponding to a group name
* @param cookieName name of the cookie which possible contains project
* names used as fallback
* @return set of project names. Possibly empty set but never {@code null}.
*/
protected SortedSet<String> getRequestedProjects(String searchAllParamName, String projectParamName, String groupParamName, String cookieName) {
TreeSet<String> projectNames = new TreeSet<>();
List<Project> projects = getEnv().getProjectList();
if (Boolean.parseBoolean(req.getParameter(searchAllParamName))) {
return getProjectHelper().getAllProjects().stream().map(Project::getName).collect(Collectors.toCollection(TreeSet::new));
}
// Use a project determined directly from the URL
if (getProject() != null && getProject().isIndexed()) {
projectNames.add(getProject().getName());
return projectNames;
}
// Use a project if there is just a single project.
if (projects.size() == 1) {
Project p = projects.get(0);
if (p.isIndexed() && authFramework.isAllowed(req, p)) {
projectNames.add(p.getName());
}
return projectNames;
}
// Add all projects which match the project parameter name values/
List<String> names = getParameterValues(projectParamName);
for (String projectName : names) {
Project project = Project.getByName(projectName);
if (project != null && project.isIndexed() && authFramework.isAllowed(req, project)) {
projectNames.add(projectName);
}
}
// Add all projects which are part of a group that matches the group parameter name.
names = getParameterValues(groupParamName);
for (String groupName : names) {
Group group = Group.getByName(groupName);
if (group != null) {
projectNames.addAll(getProjectHelper().getAllGrouped(group).stream().filter(Project::isIndexed).map(Project::getName).collect(Collectors.toSet()));
}
}
// Add projects based on cookie.
if (projectNames.isEmpty() && getIntParam(QueryParameters.NUM_SELECTED_PARAM, -1) != 0) {
List<String> cookies = getCookieVals(cookieName);
for (String s : cookies) {
Project x = Project.getByName(s);
if (x != null && x.isIndexed() && authFramework.isAllowed(req, x)) {
projectNames.add(s);
}
}
}
// Add default projects.
if (projectNames.isEmpty()) {
Set<Project> defaultProjects = env.getDefaultProjects();
if (defaultProjects != null) {
for (Project project : defaultProjects) {
if (project.isIndexed() && authFramework.isAllowed(req, project)) {
projectNames.add(project.getName());
}
}
}
}
return projectNames;
}
use of org.opengrok.indexer.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 occurred and one should not try to render a view.
*/
public DiffData getDiffData() {
DiffData data = new DiffData(getPath().substring(0, getPath().lastIndexOf(PATH_SEPARATOR)), Util.htmlize(getResourceFile().getName()));
String srcRoot = getSourceRootPath();
String context = req.getContextPath();
String[] filepath = new String[2];
if (!getFileRevision(data, context, filepath)) {
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.
Future<?>[] future = new Future<?>[2];
for (int i = 0; i < 2; i++) {
File f = new File(srcRoot + filepath[i]);
final String revision = data.rev[i];
future[i] = executor.submit(() -> HistoryGuru.getInstance().getRevision(f.getParent(), f.getName(), revision));
}
for (int i = 0; i < 2; i++) {
// The Executor used by given repository will enforce the timeout.
in[i] = (InputStream) future[i].get();
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 != AbstractAnalyzer.Genre.PLAIN && data.genre != AbstractAnalyzer.Genre.HTML) {
return data;
}
ArrayList<String> lines = new ArrayList<>();
Project p = getProject();
for (int i = 0; i < 2; i++) {
// All files under source root are 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[0]);
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;
}
use of org.opengrok.indexer.configuration.Project in project OpenGrok by OpenGrok.
the class ProjectsController method getRepositories.
@GET
@Path("/{project}/repositories")
@Produces(MediaType.APPLICATION_JSON)
public List<String> getRepositories(@PathParam("project") String projectName) {
// Avoid classification as a taint bug.
projectName = Laundromat.launderInput(projectName);
Project project = env.getProjects().get(projectName);
if (project != null) {
List<RepositoryInfo> infos = env.getProjectRepositoriesMap().get(project);
if (infos != null) {
return infos.stream().map(RepositoryInfo::getDirectoryNameRelative).collect(Collectors.toList());
}
}
return Collections.emptyList();
}
Aggregations