Search in sources :

Example 1 with FileApiUrlBuilder

use of jetbrains.buildServer.server.rest.model.files.FileApiUrlBuilder in project teamcity-rest by JetBrains.

the class FilesSubResource method getZipped.

@GET
@Path("/archived" + "{path:(/.*)?}")
@Produces({ MediaType.WILDCARD })
@ApiOperation(value = "Get specific file zipped.", nickname = "getZippedFile")
public Response getZipped(@PathParam("path") final String path, @QueryParam("basePath") final String basePath, @QueryParam("locator") final String locator, @QueryParam("name") final String name, @Context HttpServletRequest request) {
    final String processedPath = myProvider.preprocess(StringUtil.removeLeadingSlash(path));
    String actualBasePath = basePath != null ? myProvider.preprocess(basePath) : processedPath;
    String finalName = myProvider.preprocess(name);
    if (StringUtil.isEmpty(finalName)) {
        finalName = myProvider.getArchiveName(processedPath) + ".zip";
    }
    // include al files recursively by default
    String actualLocator = Locator.setDimensionIfNotPresent(locator, BuildArtifactsFinder.DIMENSION_RECURSIVE, "true");
    // do not expand archives by default
    actualLocator = Locator.setDimensionIfNotPresent(actualLocator, BuildArtifactsFinder.ARCHIVES_DIMENSION_NAME, "false");
    final FileApiUrlBuilder urlBuilder = fileApiUrlBuilder(locator, myUrlPrefix);
    final List<ArtifactTreeElement> elements = BuildArtifactsFinder.getItems(myProvider.getElement(processedPath), actualBasePath, actualLocator, urlBuilder, myBeanContext.getServiceLocator());
    final ArchiveElement archiveElement = new ArchiveElement(elements, finalName);
    final Response.ResponseBuilder builder = getContentByStream(archiveElement, request, new StreamingOutputProvider() {

        @Override
        public boolean isRangeSupported() {
            return false;
        }

        @Override
        public StreamingOutput getStreamingOutput(@Nullable final Long startOffset, @Nullable final Long length) {
            return archiveElement.getStreamingOutput(startOffset, length, () -> "request " + WebUtil.getRequestDump(request));
        }
    });
    for (ArtifactTreeElement element : elements) {
        if (!myProvider.fileContentServed(Util.concatenatePath(actualBasePath, element.getFullName()), request))
            break;
    }
    // see jetbrains.buildServer.web.util.WebUtil.addCacheHeadersForIE and http://youtrack.jetbrains.com/issue/TW-9821 for details)
    if (WebUtil.isIE10OrLower(request)) {
        builder.header("Cache-Control", "private,must-revalidate");
        builder.header("Pragma", "private");
    }
    return builder.build();
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) FileApiUrlBuilder(jetbrains.buildServer.server.rest.model.files.FileApiUrlBuilder) ArtifactTreeElement(jetbrains.buildServer.web.artifacts.browser.ArtifactTreeElement) ArchiveElement(jetbrains.buildServer.server.rest.data.ArchiveElement) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with FileApiUrlBuilder

use of jetbrains.buildServer.server.rest.model.files.FileApiUrlBuilder in project teamcity-rest by JetBrains.

the class Build method getArtifacts.

@XmlElement(name = "artifacts")
public Files getArtifacts() {
    return ValueWithDefault.decideDefaultIgnoringAccessDenied(myFields.isIncluded("artifacts", false), new ValueWithDefault.Value<Files>() {

        public Files get() {
            final Fields nestedFields = myFields.getNestedField("artifacts");
            final String childrenLocator = nestedFields.getLocator();
            final FileApiUrlBuilder builder = FilesSubResource.fileApiUrlBuilder(childrenLocator, BuildRequest.getBuildArtifactsHref(myBuildPromotion));
            return new Files(builder.getChildrenHref(null), new Files.DefaultFilesProvider(builder, myBeanContext) {

                @NotNull
                @Override
                protected List<? extends Element> getItems() {
                    return BuildArtifactsFinder.getItems(BuildArtifactsFinder.getArtifactElement(myBuildPromotion, "", myServiceLocator), childrenLocator, builder, myServiceLocator);
                }

                @Override
                public int getCount() {
                    if (myItems != null) {
                        return myItems.size();
                    }
                    Integer cheapCount = getCheapCount();
                    if (cheapCount != null) {
                        return cheapCount;
                    }
                    return super.getCount();
                }

                @Override
                public boolean isCountCheap() {
                    if (super.isCountCheap())
                        return true;
                    return getCheapCount() != null;
                }

                private Integer myCachedCheapCount = null;

                private boolean myCheapCountIsCalculated = false;

                @Nullable
                private Integer getCheapCount() {
                    if (!myCheapCountIsCalculated) {
                        myCachedCheapCount = getCheapCountInternal();
                        myCheapCountIsCalculated = true;
                    }
                    return myCachedCheapCount;
                }

                @Nullable
                private Integer getCheapCountInternal() {
                    if (!((BuildPromotionEx) myBuildPromotion).hasComputedArtifactsState())
                        return null;
                    // optimize response by using cached artifacts presence
                    BuildPromotionEx.ArtifactsState state = ((BuildPromotionEx) myBuildPromotion).getArtifactStateInfo().getState();
                    if (state == BuildPromotionEx.ArtifactsState.NO_ARTIFACTS)
                        return 0;
                    Integer requestedCount = BuildArtifactsFinder.getCountIfDefaultLocator(childrenLocator);
                    // not default locator
                    if (requestedCount == null)
                        return null;
                    if (state == BuildPromotionEx.ArtifactsState.HIDDEN_ONLY)
                        return 0;
                    if (requestedCount == 1 && state == BuildPromotionEx.ArtifactsState.HAS_ARTIFACTS)
                        return 1;
                    return null;
                }
            }, nestedFields, myBeanContext);
        }
    });
}
Also used : FileApiUrlBuilder(jetbrains.buildServer.server.rest.model.files.FileApiUrlBuilder) ValueWithDefault(jetbrains.buildServer.server.rest.util.ValueWithDefault) Files(jetbrains.buildServer.server.rest.model.files.Files) XmlElement(javax.xml.bind.annotation.XmlElement)

Example 3 with FileApiUrlBuilder

use of jetbrains.buildServer.server.rest.model.files.FileApiUrlBuilder in project teamcity-rest by JetBrains.

the class FilesSubResource method getChildren.

@GET
@Path(FilesSubResource.CHILDREN + "{path:(/.*)?}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@ApiOperation(value = "getChildren", hidden = true)
public Files getChildren(@PathParam("path") @DefaultValue("") final String path, @QueryParam("basePath") final String basePath, @QueryParam("locator") final String locator, @QueryParam("fields") String fields) {
    if (!myArchiveBrowsingSupported && locator != null) {
        final Boolean browseArchives = new Locator(locator).getSingleDimensionValueAsBoolean(BuildArtifactsFinder.ARCHIVES_DIMENSION_NAME);
        if (browseArchives != null && browseArchives) {
            throw new BadRequestException("Archive browsing is not supported for this request, remove '" + BuildArtifactsFinder.ARCHIVES_DIMENSION_NAME + "' dimension");
        }
    }
    final FileApiUrlBuilder builder = fileApiUrlBuilder(locator, myUrlPrefix);
    final Element rootElement = myProvider.getElement(myProvider.preprocess(StringUtil.removeLeadingSlash(path)));
    return new Files(null, new Files.DefaultFilesProvider(builder, myBeanContext) {

        @Override
        @NotNull
        public List<? extends Element> getItems() {
            return BuildArtifactsFinder.getItems(rootElement, myProvider.preprocess(basePath), locator, builder, myBeanContext.getServiceLocator());
        }
    }, new Fields(fields), myBeanContext);
}
Also used : Locator(jetbrains.buildServer.server.rest.data.Locator) FileApiUrlBuilder(jetbrains.buildServer.server.rest.model.files.FileApiUrlBuilder) Fields(jetbrains.buildServer.server.rest.model.Fields) ArchiveElement(jetbrains.buildServer.server.rest.data.ArchiveElement) Element(jetbrains.buildServer.util.browser.Element) ArtifactElement(jetbrains.buildServer.web.artifacts.browser.ArtifactElement) ArtifactTreeElement(jetbrains.buildServer.web.artifacts.browser.ArtifactTreeElement) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) List(java.util.List) Files(jetbrains.buildServer.server.rest.model.files.Files) NotNull(org.jetbrains.annotations.NotNull) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

FileApiUrlBuilder (jetbrains.buildServer.server.rest.model.files.FileApiUrlBuilder)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ArchiveElement (jetbrains.buildServer.server.rest.data.ArchiveElement)2 Files (jetbrains.buildServer.server.rest.model.files.Files)2 ArtifactTreeElement (jetbrains.buildServer.web.artifacts.browser.ArtifactTreeElement)2 List (java.util.List)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 XmlElement (javax.xml.bind.annotation.XmlElement)1 Locator (jetbrains.buildServer.server.rest.data.Locator)1 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)1 Fields (jetbrains.buildServer.server.rest.model.Fields)1 ValueWithDefault (jetbrains.buildServer.server.rest.util.ValueWithDefault)1 Element (jetbrains.buildServer.util.browser.Element)1 ArtifactElement (jetbrains.buildServer.web.artifacts.browser.ArtifactElement)1 NotNull (org.jetbrains.annotations.NotNull)1