Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResultRenderProvider returns list instead of optional URL #2018

Merged
merged 5 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ public Stream<ExecutionStatus> getQueriesFiltered(Dataset datasetId, UriBuilder
public static <S extends ExecutionStatus> S setDownloadUrls(S status, List<ResultRendererProvider> renderer, ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders) {

List<URL> resultUrls = renderer.stream()
.map(r -> r.generateResultURL(exec, uriBuilder.clone(), allProviders))
.flatMap(Optional::stream).collect(Collectors.toList());
.map(r -> r.generateResultURLs(exec, uriBuilder.clone(), allProviders))
.flatMap(Collection::stream).collect(Collectors.toList());

status.setResultUrls(resultUrls);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.bakdata.conquery.io.jackson.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import javax.ws.rs.core.MediaType;
import java.io.IOException;

public class MediaTypeDeserializer extends JsonDeserializer<MediaType> {
@Override
public MediaType deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
return MediaType.valueOf(jsonParser.getText());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import javax.ws.rs.core.UriBuilder;
import java.net.URL;
import java.util.Optional;
import java.util.Collection;

@CPSBase
@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, property = "type")
Expand All @@ -22,7 +22,7 @@ public interface ResultRendererProvider {
* @param allProviders A flag that should override internal "hide-this-url" flags.
* @return An Optional with the url or an empty optional.
*/
Optional<URL> generateResultURL(ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders);
Collection<URL> generateResultURLs(ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public/private/protected?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ist ein public Interface, also automatisch public ;)


void registerResultResource(JerseyEnvironment environment, ManagerNode manager);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,28 @@
import lombok.extern.slf4j.Slf4j;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

@Slf4j
public class ResultUtil {

public static enum ContentDispositionOption {
// Try to display payload in the browser
INLINE,
// Force download of the payload by the browser
ATTACHMENT;

String getHeaderValue() {
return this.name().toLowerCase(Locale.ROOT);
}
}


public static ExternalEntityId createId(Namespace namespace, EntityResult cer, IdMappingConfig idMappingConfig, IdMappingState mappingState) {
EncodedDictionary dict = namespace.getStorage().getPrimaryDictionary();
Expand All @@ -32,12 +46,13 @@ public static ExternalEntityId createId(Namespace namespace, EntityResult cer, I
mappingState);
}

public static Response makeResponseWithFileName(StreamingOutput out, String label, String fileExtension) {
public static Response makeResponseWithFileName(StreamingOutput out, String label, String fileExtension, MediaType mediaType, ContentDispositionOption disposition) {
Response.ResponseBuilder response = Response.ok(out);
response.header(HttpHeaders.CONTENT_TYPE, mediaType);
if(!(Strings.isNullOrEmpty(label) || label.isBlank())) {
// Set filename from label if the label was set, otherwise the browser will name the file according to the request path
response.header("Content-Disposition", String.format(
"attachment; filename=\"%s\"",FileUtil.makeSafeFileName(label, fileExtension)));
"%s; filename=\"%s\"", disposition.getHeaderValue(), FileUtil.makeSafeFileName(label, fileExtension)));
}
return response.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.arrow.vector.dictionary.DictionaryProvider;
import org.apache.arrow.vector.ipc.ArrowFileWriter;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.nio.channels.Channels;

Expand All @@ -19,6 +20,8 @@
@RequiredArgsConstructor
public class ResultArrowFileProcessor {

// From https://www.iana.org/assignments/media-types/application/vnd.apache.arrow.file
public static final MediaType MEDIA_TYPE = new MediaType("application", "vnd.apache.arrow.file");
private final DatasetRegistry datasetRegistry;
private final ConqueryConfig config;

Expand All @@ -31,6 +34,7 @@ public <E extends ManagedExecution<?> & SingleTableResult> Response getArrowFile
datasetRegistry,
pretty,
FILE_EXTENTION_ARROW_FILE,
MEDIA_TYPE,
config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import static com.bakdata.conquery.io.result.ResultUtil.makeResponseWithFileName;
import static com.bakdata.conquery.io.result.arrow.ArrowRenderer.renderToStream;
import static com.bakdata.conquery.models.auth.AuthorizationHelper.authorizeDownloadDatasets;
import static com.bakdata.conquery.resources.ResourceConstants.FILE_EXTENTION_ARROW_FILE;
import static com.bakdata.conquery.resources.ResourceConstants.FILE_EXTENTION_ARROW_STREAM;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.util.function.Function;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

Expand All @@ -32,13 +30,9 @@
import com.bakdata.conquery.models.worker.DatasetRegistry;
import com.bakdata.conquery.models.worker.Namespace;
import com.bakdata.conquery.util.io.ConqueryMDC;
import lombok.RequiredArgsConstructor;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.arrow.vector.VectorSchemaRoot;
import org.apache.arrow.vector.dictionary.DictionaryProvider;
import org.apache.arrow.vector.ipc.ArrowFileWriter;
import org.apache.arrow.vector.ipc.ArrowStreamWriter;
import org.apache.arrow.vector.ipc.ArrowWriter;
import org.apache.http.HttpStatus;

Expand All @@ -55,6 +49,7 @@ public static <E extends ManagedExecution<?> & SingleTableResult> Response getAr
DatasetRegistry datasetRegistry,
boolean pretty,
String fileExtension,
MediaType mediaType,
ConqueryConfig config) {

final Namespace namespace = datasetRegistry.get(dataset.getId());
Expand Down Expand Up @@ -99,7 +94,7 @@ public void write(OutputStream output) throws IOException, WebApplicationExcepti
}
};

return makeResponseWithFileName(out, exec.getLabelWithoutAutoLabelSuffix(), fileExtension);
return makeResponseWithFileName(out, exec.getLabelWithoutAutoLabelSuffix(), fileExtension, mediaType, ResultUtil.ContentDispositionOption.attachment);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.arrow.vector.dictionary.DictionaryProvider;
import org.apache.arrow.vector.ipc.ArrowStreamWriter;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import static com.bakdata.conquery.io.result.arrow.ResultArrowProcessor.getArrowResult;
Expand All @@ -19,6 +20,8 @@
public class ResultArrowStreamProcessor {


// From https://www.iana.org/assignments/media-types/application/vnd.apache.arrow.stream
public static final MediaType MEDIA_TYPE = new MediaType("application", "vnd.apache.arrow.stream");
private final DatasetRegistry datasetRegistry;
private final ConqueryConfig config;

Expand All @@ -32,6 +35,7 @@ public <E extends ManagedExecution<?> & SingleTableResult> Response getArrowStre
datasetRegistry,
pretty,
FILE_EXTENTION_ARROW_STREAM,
MEDIA_TYPE,
config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.charset.Charset;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

Expand Down Expand Up @@ -74,7 +75,7 @@ public <E extends ManagedExecution<?> & SingleTableResult> Response getResult(Us
throw new WebApplicationException("Failed to load result", e);
}
};
return makeResponseWithFileName(out, exec.getLabelWithoutAutoLabelSuffix(), "csv");
return makeResponseWithFileName(out, exec.getLabelWithoutAutoLabelSuffix(), "csv", new MediaType("text", "csv", charset.toString()), ResultUtil.ContentDispositionOption.attachment);
}

}
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
package com.bakdata.conquery.io.result.excel;

import com.bakdata.conquery.io.result.ResultUtil;
import com.bakdata.conquery.io.result.excel.ExcelRenderer;
import com.bakdata.conquery.models.auth.entities.User;
import com.bakdata.conquery.models.auth.permissions.Ability;
import com.bakdata.conquery.models.config.ConqueryConfig;
import com.bakdata.conquery.models.datasets.Dataset;
import com.bakdata.conquery.models.execution.ManagedExecution;
import com.bakdata.conquery.models.i18n.I18n;
import com.bakdata.conquery.models.identifiable.ids.specific.DatasetId;
import com.bakdata.conquery.models.identifiable.ids.specific.ManagedExecutionId;
import com.bakdata.conquery.models.identifiable.mapping.IdMappingConfig;
import com.bakdata.conquery.models.identifiable.mapping.IdMappingState;
import com.bakdata.conquery.models.query.PrintSettings;
import com.bakdata.conquery.models.query.SingleTableResult;
import com.bakdata.conquery.models.query.results.EntityResult;
import com.bakdata.conquery.models.worker.DatasetRegistry;
import com.bakdata.conquery.models.worker.Namespace;
import com.bakdata.conquery.util.ResourceUtil;
import com.bakdata.conquery.util.io.ConqueryMDC;
import lombok.RequiredArgsConstructor;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import static com.bakdata.conquery.io.result.ResultUtil.checkSingleTableResult;
import static com.bakdata.conquery.io.result.ResultUtil.makeResponseWithFileName;

@RequiredArgsConstructor
public class ResultExcelProcessor {

// Media type according to https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
public static final MediaType MEDIA_TYPE = new MediaType("application", "vnd.openxmlformats-officedocument.spreadsheetml.sheet");
private final DatasetRegistry datasetRegistry;
private final ConqueryConfig config;

Expand Down Expand Up @@ -62,7 +60,7 @@ public <E extends ManagedExecution<?> & SingleTableResult> Response getExcelResu
output
);

return makeResponseWithFileName(out, exec.getLabelWithoutAutoLabelSuffix(), "xlsx");
return makeResponseWithFileName(out, exec.getLabelWithoutAutoLabelSuffix(), "xlsx", MEDIA_TYPE, ResultUtil.ContentDispositionOption.attachment);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import javax.ws.rs.core.UriBuilder;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Getter
Expand All @@ -25,16 +28,16 @@ public class ArrowFileResultProvider implements ResultRendererProvider {

@Override
@SneakyThrows(MalformedURLException.class)
public Optional<URL> generateResultURL(ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders) {
public Collection<URL> generateResultURLs(ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders) {
if (!(exec instanceof SingleTableResult)) {
return Optional.empty();
return Collections.emptyList();
}

if (hidden && !allProviders) {
return Optional.empty();
return Collections.emptyList();
}

return Optional.of(ResultArrowFileResource.getDownloadURL(uriBuilder, (ManagedExecution<?> & SingleTableResult) exec));
return List.of(ResultArrowFileResource.getDownloadURL(uriBuilder, (ManagedExecution<?> & SingleTableResult) exec));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import javax.ws.rs.core.UriBuilder;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Getter
Expand All @@ -25,16 +28,16 @@ public class ArrowStreamResultProvider implements ResultRendererProvider {

@Override
@SneakyThrows(MalformedURLException.class)
public Optional<URL> generateResultURL(ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders) {
public Collection<URL> generateResultURLs(ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders) {
if (!(exec instanceof SingleTableResult)) {
return Optional.empty();
return Collections.emptyList();
}

if (hidden && !allProviders) {
return Optional.empty();
return Collections.emptyList();
}

return Optional.of(ResultArrowStreamResource.getDownloadURL(uriBuilder, (ManagedExecution<?> & SingleTableResult) exec));
return List.of(ResultArrowStreamResource.getDownloadURL(uriBuilder, exec));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import javax.ws.rs.core.UriBuilder;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Getter
Expand All @@ -25,16 +28,16 @@ public class CsvResultRendererProvider implements ResultRendererProvider {

@Override
@SneakyThrows(MalformedURLException.class)
public Optional<URL> generateResultURL(ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders) {
public Collection<URL> generateResultURLs(ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders) {
if (!(exec instanceof SingleTableResult)) {
return Optional.empty();
return Collections.emptyList();
}

if (hidden && !allProviders) {
return Optional.empty();
return Collections.emptyList();
}

return Optional.of(ResultCsvResource.getDownloadURL(uriBuilder, (ManagedExecution<?> & SingleTableResult) exec));
return List.of(ResultCsvResource.getDownloadURL(uriBuilder, (ManagedExecution<?> & SingleTableResult) exec));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import javax.ws.rs.core.UriBuilder;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Getter
Expand All @@ -25,16 +28,16 @@ public class XlsxResultProvider implements ResultRendererProvider {

@Override
@SneakyThrows(MalformedURLException.class)
public Optional<URL> generateResultURL(ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders) {
public Collection<URL> generateResultURLs(ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders) {
if (!(exec instanceof SingleTableResult)) {
return Optional.empty();
return Collections.emptyList();
}

if (hidden && !allProviders) {
return Optional.empty();
return Collections.emptyList();
}

return Optional.of(ResultExcelResource.getDownloadURL(uriBuilder, (ManagedExecution<?> & SingleTableResult) exec));
return List.of(ResultExcelResource.getDownloadURL(uriBuilder, (ManagedExecution<?> & SingleTableResult) exec));
}

@Override
Expand Down