Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions datamodel/odata-v4/odata-v4-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>com.sun.codemodel</groupId>
<artifactId>codemodel</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -33,7 +36,6 @@
import org.apache.olingo.commons.api.edm.provider.CsdlTerm;
import org.apache.olingo.commons.api.format.ContentType;
import org.slf4j.Logger;
import org.springframework.util.AntPathMatcher;

import com.google.common.collect.Multimap;
import com.sap.cloud.sdk.cloudplatform.util.StringUtils;
Expand Down Expand Up @@ -392,16 +394,14 @@ private String getCanonicalPath( @Nullable final File outputDir )
private boolean excludePatternMatch( final String excludeFilePattern, final String serviceMetadataFilename )
{
final List<String> excludeFilePatternEach = new ArrayList<>(Arrays.asList(excludeFilePattern.split(",")));
final AntPathMatcher antPathMatcher = new AntPathMatcher();
final FileSystem fileSystem = FileSystems.getDefault();
Copy link
Contributor

Choose a reason for hiding this comment

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


for( final String filePattern : excludeFilePatternEach ) {
if( antPathMatcher.match(filePattern, serviceMetadataFilename) ) {
logger
.info(
String
.format(
"Excluding metadata file %s, as it matches with the excludes pattern.",
serviceMetadataFilename));
final PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:" + filePattern.trim());

if( pathMatcher.matches(Paths.get(serviceMetadataFilename)) ) {
final String msg = "Excluding metadata file %s, as it matches with the excludes pattern.";
logger.info(String.format(msg, serviceMetadataFilename));
return true;
}
}
Expand Down
4 changes: 0 additions & 4 deletions datamodel/odata/odata-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>com.sun.codemodel</groupId>
<artifactId>codemodel</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -29,7 +32,6 @@
import org.apache.olingo.odata2.core.edm.provider.EdmImplProv;
import org.apache.olingo.odata2.core.edm.provider.EdmxProvider;
import org.slf4j.Logger;
import org.springframework.util.AntPathMatcher;

import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
Expand Down Expand Up @@ -349,19 +351,17 @@ private String getCanonicalPath( @Nullable final File outputDir )
}
}

private boolean excludePatternMatch( final String excludeFilePattern, final String serviceMetadataFilename )
static boolean excludePatternMatch( final String excludeFilePattern, final String serviceMetadataFilename )
{
final List<String> excludeFilePatternEach = new ArrayList<>(Arrays.asList(excludeFilePattern.split(",")));
final AntPathMatcher antPathMatcher = new AntPathMatcher();
final FileSystem fileSystem = FileSystems.getDefault();

for( final String filePattern : excludeFilePatternEach ) {
if( antPathMatcher.match(filePattern, serviceMetadataFilename) ) {
logger
.info(
String
.format(
"Excluding metadata file %s, as it matches with the excludes pattern.",
serviceMetadataFilename));
final PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:" + filePattern.trim());

if( pathMatcher.matches(Paths.get(serviceMetadataFilename)) ) {
final String msg = "Excluding metadata file %s, as it matches with the excludes pattern.";
logger.info(String.format(msg, serviceMetadataFilename));
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.sap.cloud.sdk.datamodel.odata.generator;

import static com.sap.cloud.sdk.datamodel.odata.generator.ODataToVdmGenerator.excludePatternMatch;

import java.io.File;

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;

public class ODataToVdmGeneratorTest
{
@Test
void testExcludePatternMatch()
{
// Get file-name only, without path
final String fileName = new File("src/test/resources/sample/Spec.edmx").getName();

final SoftAssertions softly = new SoftAssertions();
for( final String pattern : new String[] { "*.edmx", "*.*", "sp*c.edmx", "*.EDMX" } )
softly
.assertThat(excludePatternMatch(pattern, fileName))
.describedAs("%s matches %s", pattern, fileName)
.isTrue();

for( final String pattern : new String[] { "F", "*/*", "/resources/*/*.edmx", "/resources/*/*" } )
softly
.assertThat(excludePatternMatch(pattern, fileName))
.describedAs("%s not matches %s", pattern, fileName)
.isFalse();

softly.assertThat(excludePatternMatch("A,B,spec.edmx", fileName)).describedAs("fallback").isTrue();
softly.assertAll();
}
}
14 changes: 14 additions & 0 deletions datamodel/openapi/openapi-api-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@
<organizationUrl>https://www.sap.com</organizationUrl>
</developer>
</developers>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring6.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring6.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.sap.cloud.sdk.datamodel</groupId>
Expand Down
35 changes: 35 additions & 0 deletions datamodel/openapi/openapi-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand All @@ -55,10 +56,40 @@
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>3.0.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>tools.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>3.0.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
Expand Down Expand Up @@ -110,6 +141,10 @@
<ignoredNonTestScopedDependencies>
<ignoredNonTestScopedDependency>com.fasterxml.jackson.core:jackson-core</ignoredNonTestScopedDependency>
</ignoredNonTestScopedDependencies>
<ignoredUnusedDeclaredDependencies>
<dependency>tools.jackson.core:jackson-core</dependency>
<dependency>org.skyscreamer:jsonassert</dependency>
</ignoredUnusedDeclaredDependencies>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -27,18 +28,13 @@
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.sap.cloud.sdk.cloudplatform.connectivity.ApacheHttpClient5Accessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
import com.sap.cloud.sdk.services.openapi.apiclient.auth.ApiKeyAuth;
Expand All @@ -51,6 +47,8 @@
*/
public final class ApiClient
{
private static final ConverterPatcher[] CONVERTER_PATCHES =
{ new ConverterPatcher.Jackson2(), new ConverterPatcher.Jackson3() };

/**
* Enum representing the delimiter of a given collection.
Expand Down Expand Up @@ -338,8 +336,7 @@ public ApiClient setUserAgent( @Nonnull final String userAgent )
@Nonnull
public ApiClient addDefaultHeader( @Nonnull final String name, @Nonnull final String value )
{
defaultHeaders.remove(name);
defaultHeaders.add(name, value);
defaultHeaders.set(name, value);
return this;
}

Expand Down Expand Up @@ -688,8 +685,7 @@ public <T> T invokeAPI(
// auth headers are added automatically by the SDK
// updateParamsForAuth(authNames, queryParams, headerParams);

@SuppressWarnings( "deprecation" ) // spring-web:6.2.0 and later, works until <7.0.0
final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(basePath).path(path);
if( queryParams != null ) {
//encode the query parameters in case they contain unsafe characters
for( final List<String> values : queryParams.values() ) {
Expand Down Expand Up @@ -724,7 +720,7 @@ public <T> T invokeAPI(
final ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);

statusCode = responseEntity.getStatusCode().value();
responseHeaders = responseEntity.getHeaders();
responseHeaders = extractHeadersMap(responseEntity.getHeaders());

if( statusCode == 204 ) {
return null;
Expand All @@ -737,6 +733,22 @@ public <T> T invokeAPI(
}
}

@SuppressWarnings( "unchecked" )
static MultiValueMap<String, String> extractHeadersMap( @Nonnull final HttpHeaders headers )
{
if( headers instanceof MultiValueMap ) {
return (MultiValueMap<String, String>) headers; // until including Spring Framework 6
}
try {
return (MultiValueMap<String, String>) HttpHeaders.class
.getDeclaredMethod("asMultiValueMap") // since Spring Framework 7
.invoke(headers);
}
catch( final Exception e ) {
return MultiValueMap.fromSingleValue(headers.toSingleValueMap()); // fallback
}
}

/**
* Add headers to the request that is being built
*
Expand All @@ -748,43 +760,22 @@ public <T> T invokeAPI(
private void addHeadersToRequest( @Nullable final HttpHeaders headers, final BodyBuilder requestBuilder )
{
if( headers != null ) {
for( final Entry<String, List<String>> entry : headers.entrySet() ) {
final List<String> values = entry.getValue();
for( final String value : values ) {
if( value != null ) {
requestBuilder.header(entry.getKey(), value);
}
}
}
requestBuilder.headers(headers);
}
}

@Nonnull
private static RestTemplate newDefaultRestTemplate()
{
final RestTemplate restTemplate = new RestTemplate();

final ObjectMapper objectMapper = newDefaultObjectMapper();
restTemplate
.getMessageConverters()
.stream()
.filter(MappingJackson2HttpMessageConverter.class::isInstance)
.map(MappingJackson2HttpMessageConverter.class::cast)
.forEach(converter -> converter.setObjectMapper(objectMapper));

final List<HttpMessageConverter<?>> converters = new LinkedList<>(restTemplate.getMessageConverters());
for( final ConverterPatcher patcher : CONVERTER_PATCHES ) {
patcher.patchList(converters);
}
restTemplate.setMessageConverters(converters);
return restTemplate;
}

@Nonnull
private static ObjectMapper newDefaultObjectMapper()
{
return new Jackson2ObjectMapperBuilder()
.modules(new JavaTimeModule())
.visibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE)
.visibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.NONE)
.build();
}

@Nonnull
private static
RestTemplate
Expand Down
Loading
Loading