Skip to content

Commit 91ff427

Browse files
committed
Fix okhttp and version extraction
1 parent a55d86e commit 91ff427

File tree

4 files changed

+74
-24
lines changed

4 files changed

+74
-24
lines changed

pom.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<groupId>com.securenative.java</groupId>
77
<artifactId>securenative-java</artifactId>
88
<packaging>jar</packaging>
9-
<version>0.4.3</version>
9+
<version>0.4.4</version>
1010
<url>https://github.com/securenative/securenative-java</url>
1111

1212
<name>${project.groupId}:${project.artifactId}:${project.version}</name>
@@ -133,7 +133,7 @@
133133
<dependency>
134134
<groupId>com.squareup.okhttp3</groupId>
135135
<artifactId>okhttp</artifactId>
136-
<version>4.1.0</version>
136+
<version>3.14.8</version>
137137
</dependency>
138138
<dependency>
139139
<groupId>org.mockito</groupId>
@@ -156,7 +156,7 @@
156156
<dependency>
157157
<groupId>com.squareup.okhttp3</groupId>
158158
<artifactId>mockwebserver</artifactId>
159-
<version>4.0.0</version>
159+
<version>3.14.8</version>
160160
<scope>test</scope>
161161
</dependency>
162162
<dependency>
@@ -200,6 +200,14 @@
200200
<groupId>org.apache.maven.plugins</groupId>
201201
<artifactId>maven-jar-plugin</artifactId>
202202
<version>2.4</version>
203+
<configuration>
204+
<archive>
205+
<manifest>
206+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
207+
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
208+
</manifest>
209+
</archive>
210+
</configuration>
203211
</plugin>
204212
<plugin>
205213
<artifactId>maven-deploy-plugin</artifactId>

src/main/java/com/securenative/http/SecureNativeHTTPClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public SecureNativeHTTPClient(SecureNativeOptions options) {
3636

3737
@Override
3838
public HttpResponse post(String path, String payload) throws IOException {
39-
RequestBody body = RequestBody.create(payload, JSON_MEDIA_TYPE);
39+
RequestBody body = RequestBody.create(JSON_MEDIA_TYPE, payload);
4040

4141
String url = String.format("%s/%s", this.options.getApiUrl(), path);
4242

src/main/java/com/securenative/utils/VersionUtils.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,53 @@
22

33
import com.securenative.ResourceStream;
44
import com.securenative.ResourceStreamImpl;
5+
import com.securenative.SecureNative;
6+
import com.securenative.config.ConfigurationManager;
57
import org.apache.maven.model.Model;
68
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
79

10+
import java.io.InputStream;
11+
import java.util.Properties;
12+
813
public class VersionUtils {
914
private static ResourceStream resourceStream = new ResourceStreamImpl();
1015

11-
public static String getVersion() {
16+
public static void setResourceStream(ResourceStream resourceStream) {
17+
VersionUtils.resourceStream = resourceStream;
18+
}
19+
20+
public static synchronized String getVersion() {
21+
String version = null;
22+
23+
// try to load from maven properties first
1224
try {
13-
MavenXpp3Reader reader = new MavenXpp3Reader();
14-
String POM_RESOURCE = "/META-INF/maven/com.securenative.java/pom.xml";
15-
Model read = reader.read(resourceStream.getInputStream(POM_RESOURCE));
16-
return read.getParent().getVersion();
25+
Properties p = new Properties();
26+
InputStream is = resourceStream.getInputStream("/META-INF/maven/com.securenative.java/securenative-java/pom.properties");
27+
if (is != null) {
28+
p.load(is);
29+
version = p.getProperty("version", "");
30+
}
1731
} catch (Exception e) {
18-
return "unknown";
32+
// ignore
1933
}
34+
35+
// fallback to using Java API
36+
if (version == null) {
37+
Package aPackage = VersionUtils.class.getPackage();
38+
if (aPackage != null) {
39+
version = aPackage.getImplementationVersion();
40+
if (version == null) {
41+
version = aPackage.getSpecificationVersion();
42+
}
43+
}
44+
}
45+
46+
if (version == null) {
47+
// we could not compute the version so use a blank
48+
version = "unknown";
49+
}
50+
51+
return version;
2052
}
2153
}
2254

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
package com.securenative.utils;
22

3+
import com.securenative.ResourceStreamImpl;
4+
import com.securenative.config.ConfigurationManager;
35
import org.junit.jupiter.api.Test;
6+
import org.mockito.Mockito;
47

8+
import java.io.ByteArrayInputStream;
59
import java.io.IOException;
6-
import java.net.JarURLConnection;
7-
import java.net.URL;
8-
import java.util.Enumeration;
9-
import java.util.jar.Attributes;
10-
import java.util.jar.Manifest;
10+
import java.io.InputStream;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
1113

1214
public class VersionTest {
1315

1416
@Test
15-
public void testManifest() throws IOException {
16-
URL res = org.junit.Assert.class.getResource(org.junit.Assert.class.getSimpleName() + ".class");
17-
JarURLConnection conn = (JarURLConnection) res.openConnection();
18-
Manifest mf = conn.getManifest();
19-
20-
Attributes atts = mf.getMainAttributes();
21-
for (Object v : atts.values()) {
22-
System.out.println(v);
23-
}
17+
public void testVersionExtraction() throws IOException {
18+
19+
String props = String.join(System.getProperty("line.separator"),
20+
"version=1.0.0",
21+
"groupId=com.securenative.java",
22+
"artifactId=securenative-java");
23+
24+
InputStream inputStream = new ByteArrayInputStream(props.getBytes());
25+
ResourceStreamImpl resourceStream = Mockito.spy(new ResourceStreamImpl());
26+
Mockito.when(resourceStream.getInputStream("/META-INF/maven/com.securenative.java/securenative-java/pom.properties")).thenReturn(inputStream);
27+
28+
VersionUtils.setResourceStream(resourceStream);
29+
30+
assertThat(VersionUtils.getVersion()).isEqualTo("1.0.0");
31+
32+
// restore resource stream
33+
VersionUtils.setResourceStream(new ResourceStreamImpl());
2434
}
2535
}

0 commit comments

Comments
 (0)