Search in sources :

Example 31 with DefaultPrettyPrinter

use of com.fasterxml.jackson.core.util.DefaultPrettyPrinter in project topcom-cloud by 545314690.

the class MyWebAppConfigurer method extendMessageConverters.

/**
 * 添加返回结果缩进支持,如果存在pretty参数,则返回结果添加缩进
 * @param converters
 */
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.replaceAll(c -> {
        if (c instanceof MappingJackson2HttpMessageConverter) {
            MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(mapper) {

                @Override
                protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
                    RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
                    if (attributes != null && attributes instanceof ServletRequestAttributes) {
                        String attribute = ((ServletRequestAttributes) attributes).getRequest().getParameter("pretty");
                        if (attribute != null) {
                            generator.setPrettyPrinter(new DefaultPrettyPrinter());
                        }
                    }
                    super.writePrefix(generator, object);
                }
            };
            return converter;
        } else {
            return c;
        }
    });
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 32 with DefaultPrettyPrinter

use of com.fasterxml.jackson.core.util.DefaultPrettyPrinter in project kylo by Teradata.

the class FilesystemRepositoryService method importTemplate.

@Override
public ImportTemplate importTemplate(String repositoryName, String repositoryType, String fileName, String uploadKey, String importComponents) throws Exception {
    Optional<TemplateRepository> repository = listRepositories().stream().filter(r -> StringUtils.equals(r.getName(), repositoryName) && StringUtils.equals(r.getType().getKey(), repositoryType)).findFirst();
    Path filePath = Paths.get(repository.get().getLocation() + "/" + fileName);
    if (!filePath.toFile().exists()) {
        throw new RuntimeException("Unable to find template file to import: " + fileName);
    }
    log.info("Begin template import {}", fileName);
    ImportTemplateOptions options = new ImportTemplateOptions();
    options.setUploadKey(uploadKey);
    byte[] content = ImportUtil.streamToByteArray(new FileInputStream(filePath.toFile()));
    String checksum = DigestUtils.md5DigestAsHex(content);
    uploadProgressService.newUpload(uploadKey);
    ImportTemplate importTemplate = null;
    TemplateImporter templateImporter = null;
    if (importComponents == null) {
        templateImporter = templateImporterFactory.apply(fileName, content, options);
        importTemplate = templateImporter.validate();
        importTemplate.setSuccess(false);
    } else {
        options.setImportComponentOptions(ObjectMapperSerializer.deserialize(importComponents, new TypeReference<Set<ImportComponentOption>>() {
        }));
        templateImporter = templateImporterFactory.apply(fileName, content, options);
        importTemplate = templateImporter.validateAndImport();
    }
    log.info("End template import {} - {}", fileName, importTemplate.isSuccess());
    // update template metadata
    String baseName = FilenameUtils.getBaseName(fileName);
    Path metadataPath = Paths.get(repository.get().getLocation() + "/" + baseName + ".json");
    TemplateMetadata templateMetadata = mapper.readValue(metadataPath.toFile(), TemplateMetadata.class);
    templateMetadata.setChecksum(checksum);
    long updateTime = importTemplate.getTemplateToImport().getUpdateDate().getTime();
    templateMetadata.setLastModified(updateTime);
    templateMetadata.setUpdateAvailable(false);
    mapper.writer(new DefaultPrettyPrinter()).writeValue(metadataPath.toFile(), templateMetadata);
    log.info("Generated checksum for {} - {}", templateMetadata.getTemplateName(), checksum);
    templateUpdateInfoCache.put(templateMetadata.getTemplateName(), false);
    return importTemplate;
}
Also used : UploadProgressService(com.thinkbiganalytics.feedmgr.service.UploadProgressService) RegisteredTemplateService(com.thinkbiganalytics.feedmgr.service.template.RegisteredTemplateService) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) TemplateImporter(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporter) StringUtils(org.apache.commons.lang3.StringUtils) TemplateChangeEvent(com.thinkbiganalytics.metadata.api.event.template.TemplateChangeEvent) ObjectMapperSerializer(com.thinkbiganalytics.json.ObjectMapperSerializer) ByteArrayInputStream(java.io.ByteArrayInputStream) ImportUtil(com.thinkbiganalytics.feedmgr.util.ImportUtil) TemplateSearchFilter(com.thinkbiganalytics.repository.api.TemplateSearchFilter) TypeReference(com.fasterxml.jackson.core.type.TypeReference) MetadataAccess(com.thinkbiganalytics.metadata.api.MetadataAccess) Path(java.nio.file.Path) RepositoryService(com.thinkbiganalytics.repository.api.RepositoryService) ResourceLoader(org.springframework.core.io.ResourceLoader) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) MetadataEventListener(com.thinkbiganalytics.metadata.api.event.MetadataEventListener) FILESYSTEM(com.thinkbiganalytics.repository.api.TemplateRepository.RepositoryType.FILESYSTEM) Set(java.util.Set) MetadataEventService(com.thinkbiganalytics.metadata.api.event.MetadataEventService) Collectors(java.util.stream.Collectors) ExportTemplate(com.thinkbiganalytics.metadata.api.template.export.ExportTemplate) List(java.util.List) Principal(java.security.Principal) Stream(java.util.stream.Stream) PostConstruct(javax.annotation.PostConstruct) ImportTemplateOptions(com.thinkbiganalytics.feedmgr.rest.model.ImportTemplateOptions) Optional(java.util.Optional) TemplateChange(com.thinkbiganalytics.metadata.api.event.template.TemplateChange) FilenameUtils(org.apache.commons.io.FilenameUtils) SearchResultImpl(com.thinkbiganalytics.rest.model.search.SearchResultImpl) TemplateComparator.valueOf(com.thinkbiganalytics.repository.api.TemplateSearchFilter.TemplateComparator.valueOf) DigestUtils(org.springframework.util.DigestUtils) PropertySource(org.springframework.context.annotation.PropertySource) Scheduled(org.springframework.scheduling.annotation.Scheduled) TemplateMetadataWrapper(com.thinkbiganalytics.repository.api.TemplateMetadataWrapper) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) Inject(javax.inject.Inject) TemplateRepository(com.thinkbiganalytics.repository.api.TemplateRepository) TemplateModelTransform(com.thinkbiganalytics.feedmgr.service.template.TemplateModelTransform) Service(org.springframework.stereotype.Service) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) MetadataChange(com.thinkbiganalytics.metadata.api.event.MetadataChange) RegisteredTemplate(com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate) TemplateImporterFactory(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporterFactory) NAME(com.thinkbiganalytics.repository.api.TemplateSearchFilter.TemplateComparator.NAME) Logger(org.slf4j.Logger) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TemplateExporter(com.thinkbiganalytics.metadata.api.template.export.TemplateExporter) FileInputStream(java.io.FileInputStream) SearchResult(com.thinkbiganalytics.rest.model.search.SearchResult) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata) Paths(java.nio.file.Paths) Cache(com.google.common.cache.Cache) Comparator(java.util.Comparator) Collections(java.util.Collections) ImportComponentOption(com.thinkbiganalytics.feedmgr.rest.model.ImportComponentOption) InputStream(java.io.InputStream) Path(java.nio.file.Path) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) TemplateRepository(com.thinkbiganalytics.repository.api.TemplateRepository) ImportTemplateOptions(com.thinkbiganalytics.feedmgr.rest.model.ImportTemplateOptions) ImportComponentOption(com.thinkbiganalytics.feedmgr.rest.model.ImportComponentOption) ImportTemplate(com.thinkbiganalytics.feedmgr.service.template.importing.model.ImportTemplate) FileInputStream(java.io.FileInputStream) TemplateImporter(com.thinkbiganalytics.feedmgr.service.template.importing.TemplateImporter) TypeReference(com.fasterxml.jackson.core.type.TypeReference) TemplateMetadata(com.thinkbiganalytics.repository.api.TemplateMetadata)

Example 33 with DefaultPrettyPrinter

use of com.fasterxml.jackson.core.util.DefaultPrettyPrinter in project rdf4j by eclipse.

the class AbstractSPARQLJSONWriter method startDocument.

@Override
public void startDocument() throws QueryResultHandlerException {
    if (!documentOpen) {
        documentOpen = true;
        headerOpen = false;
        headerComplete = false;
        tupleVariablesFound = false;
        firstTupleWritten = false;
        linksFound = false;
        if (getWriterConfig().get(BasicWriterSettings.PRETTY_PRINT)) {
            // SES-2011: Always use \n for consistency
            Indenter indenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
            // By default Jackson does not pretty print, so enable this unless
            // PRETTY_PRINT setting is disabled
            DefaultPrettyPrinter pp = new DefaultPrettyPrinter().withArrayIndenter(indenter).withObjectIndenter(indenter);
            jg.setPrettyPrinter(pp);
        }
        try {
            if (getWriterConfig().isSet(BasicQueryWriterSettings.JSONP_CALLBACK)) {
                // SES-1019 : Write the callbackfunction name as a wrapper for
                // the results here
                String callbackName = getWriterConfig().get(BasicQueryWriterSettings.JSONP_CALLBACK);
                jg.writeRaw(callbackName);
                jg.writeRaw("(");
            }
            jg.writeStartObject();
        } catch (IOException e) {
            throw new QueryResultHandlerException(e);
        }
    }
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) Indenter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter.Indenter) DefaultIndenter(com.fasterxml.jackson.core.util.DefaultIndenter) IOException(java.io.IOException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException)

Example 34 with DefaultPrettyPrinter

use of com.fasterxml.jackson.core.util.DefaultPrettyPrinter in project jackson-core by FasterXML.

the class CoreJDKSerializabilityTest method testPrettyPrinter.

public void testPrettyPrinter() throws Exception {
    PrettyPrinter p = new DefaultPrettyPrinter();
    byte[] stuff = jdkSerialize(p);
    PrettyPrinter back = jdkDeserialize(stuff);
    // what should we test?
    assertNotNull(back);
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) PrettyPrinter(com.fasterxml.jackson.core.PrettyPrinter)

Example 35 with DefaultPrettyPrinter

use of com.fasterxml.jackson.core.util.DefaultPrettyPrinter in project jackson-core by FasterXML.

the class TestPrettyPrinter method testCustomRootSeparatorWithPP.

// [Issue#26]
public void testCustomRootSeparatorWithPP() throws Exception {
    JsonFactory f = new JsonFactory();
    // first, no pretty-printing (will still separate root values with a space!)
    assertEquals("{} {} []", _generateRoot(f, null));
    // First with default pretty printer, default configs:
    assertEquals("{ } { } [ ]", _generateRoot(f, new DefaultPrettyPrinter()));
    // then custom:
    assertEquals("{ }|{ }|[ ]", _generateRoot(f, new DefaultPrettyPrinter("|")));
}
Also used : DefaultPrettyPrinter(com.fasterxml.jackson.core.util.DefaultPrettyPrinter) JsonFactory(com.fasterxml.jackson.core.json.JsonFactory)

Aggregations

DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)42 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)10 IOException (java.io.IOException)9 File (java.io.File)7 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)6 DefaultIndenter (com.fasterxml.jackson.core.util.DefaultIndenter)6 OpenApiContext (io.swagger.v3.oas.integration.api.OpenApiContext)6 OpenAPI (io.swagger.v3.oas.models.OpenAPI)6 SwaggerConfiguration (io.swagger.v3.oas.integration.SwaggerConfiguration)4 PrettyPrinter (com.fasterxml.jackson.core.PrettyPrinter)3 JsonFactory (com.fasterxml.jackson.core.json.JsonFactory)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)3 OpenAPISpecFilter (io.swagger.v3.core.filter.OpenAPISpecFilter)3 SpecFilter (io.swagger.v3.core.filter.SpecFilter)3 OpenApiConfigurationException (io.swagger.v3.oas.integration.OpenApiConfigurationException)3 BufferedWriter (java.io.BufferedWriter)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3