/* * Sonatype Nexus (TM) Open Source Version * Copyright (c) 2008-present Sonatype, Inc. * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions. * * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0, * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html. * * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the * Eclipse Foundation. All other trademarks are the property of their respective owners. */ package org.sonatype.nexus.repository.rest.internal.resources;
/** * @since 3.6 */ @Named @Singleton @Path(RepositoryBrowseResource.RESOURCE_URI) @Produces(TEXT_HTML) public class RepositoryBrowseResource extends ComponentSupport implements Resource { private static final String REPOSITORY_PATH_SEGMENT = "{noop: (/)?}{repositoryPath: ((?<=/).*)?}";
public static final String RESOURCE_URI = "/repository/browse/{repositoryName}" + REPOSITORY_PATH_SEGMENT;
private static final String TEMPLATE_RESOURCE = "browseContentHtml.vm";
private final RepositoryManager repositoryManager;
private final BrowseNodeQueryService browseNodeQueryService;
private final BrowseNodeConfiguration configuration;
private final TemplateHelper templateHelper;
private final SecurityHelper securityHelper;
private final URL template;
@Inject public RepositoryBrowseResource( final RepositoryManager repositoryManager, final BrowseNodeQueryService browseNodeQueryService, final BrowseNodeConfiguration configuration, final TemplateHelper templateHelper, final SecurityHelper securityHelper) { this.repositoryManager = checkNotNull(repositoryManager); this.browseNodeQueryService = checkNotNull(browseNodeQueryService); this.configuration = checkNotNull(configuration); this.templateHelper = checkNotNull(templateHelper); this.securityHelper = checkNotNull(securityHelper); this.template = getClass().getResource(TEMPLATE_RESOURCE); checkNotNull(template); }
@GET public Response getHtml(@PathParam("repositoryName") final String repositoryName, @PathParam("repositoryPath") final String repositoryPath, @Context final UriInfo uriInfo) { log.debug("Get HTML directory listing for repository {} on path {}", repositoryName, repositoryPath);
if (!uriInfo.getAbsolutePath().toString().endsWith("/")) { log.debug("Request does include a trailing slash, redirecting to include it"); return Response.seeOther(UriBuilder.fromUri(uriInfo.getAbsolutePath()).path("/").build()).build(); }
final boolean permitted = securityHelper.allPermitted(new RepositoryViewPermission(repository, BROWSE)); final boolean hasChildren = browseNodes != null && !Iterables.isEmpty(browseNodes); final List<BrowseListItem> listItems = hasChildren ? browseNodeQueryService.toListItems(repository, browseNodes) : Collections.emptyList();
//if there are visible children return them, or if we are at the root node and permitted to browse the repo if (hasChildren || (isRoot(repositoryPath) && permitted)) { return Response .ok(templateHelper.render(template, initializeTemplateParameters(repositoryName, repositoryPath, listItems))) .build(); }
private WebApplicationException createNotFoundException(final String repositoryName, final String repositoryPath) { if (repositoryPath == null) { log.debug("Requested repository could not be located or user does not have permission: {} ", repositoryName); return new WebApplicationException("Repository not found", NOT_FOUND); } else { log.debug("Requested path {} could not be located in repository {}", repositoryPath, repositoryName); return new WebApplicationException("Path not found", NOT_FOUND); } }
private TemplateParameters initializeTemplateParameters(final String repositoryName, final String path, final List<BrowseListItem> listItems) { TemplateParameters templateParameters = templateHelper.parameters();
if (isRoot(path)) { templateParameters.set("root", true); }
@GET public Response getHtml(@PathParam("repositoryName") final String repositoryName, @PathParam("repositoryPath") final String repositoryPath, @Context final UriInfo uriInfo) { log.debug("Get HTML directory listing for repository {} on path {}", repositoryName, repositoryPath);
if (!uriInfo.getAbsolutePath().toString().endsWith("/")) { log.debug("Request does include a trailing slash, redirecting to include it"); return Response.seeOther(UriBuilder.fromUri(uriInfo.getAbsolutePath()).path("/").build()).build(); }
final boolean permitted = securityHelper.allPermitted(new RepositoryViewPermission(repository, BROWSE)); final boolean hasChildren = browseNodes != null && !Iterables.isEmpty(browseNodes); final List<BrowseListItem> listItems = hasChildren ? browseNodeQueryService.toListItems(repository, browseNodes) : Collections.emptyList(); Collections.reverse(listItems); //if there are visible children return them, or if we are at the root node and permitted to browse the repo if (hasChildren || (isRoot(repositoryPath) && permitted)) { return Response .ok(templateHelper.render(template, initializeTemplateParameters(repositoryName, repositoryPath, listItems))) .build(); }