Skip to content

Commit

Permalink
Merge pull request #113 from VishalMCF/Add-User-Agent-Header
Browse files Browse the repository at this point in the history
addressed comments and removed vulnerability
  • Loading branch information
Cliftonz authored Dec 31, 2023
2 parents a62111b + 8d89fd9 commit 5a6d780
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,10 @@
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.9.6</version>
</dependency>
</dependencies>
</project>
3 changes: 2 additions & 1 deletion src/main/java/co/novu/common/base/NovuConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
@Getter
@Setter
public class NovuConfig {

public NovuConfig(String apiKey) {
this.apiKey = apiKey;
}

private String apiKey;
private String baseUrl = "https://api.novu.co/v1/";

}
24 changes: 24 additions & 0 deletions src/main/java/co/novu/common/rest/RestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@RequiredArgsConstructor
@Slf4j
public class RestHandler {

private final NovuConfig novuConfig;
Expand All @@ -30,6 +38,7 @@ public Retrofit buildRetrofit() {
Request request = chain.request()
.newBuilder()
.addHeader("Authorization", "ApiKey " + novuConfig.getApiKey())
.addHeader("User-Agent", "novu/Java@" + loadSdkVersionFromPom())
.build();
return chain.proceed(request);
}).addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC));
Expand Down Expand Up @@ -61,4 +70,19 @@ public <T, R> R extractResponse(Response<T> response, R body) throws NovuNetwork
throw new NovuNetworkException(response.errorBody() != null ? response.errorBody().string() : "Error connecting to Novu API");
}
}

private String loadSdkVersionFromPom() {
try {
InputStream inputStream = this.getClass().getResourceAsStream("/META-INF/maven/co.novu/novu-java/pom.xml");
if (inputStream == null) {
return "";
}
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new InputStreamReader(inputStream));
return model.getVersion();
} catch (Exception e) {
log.error("Could not retrieve the sdk version", e);
}
return "";
}
}

0 comments on commit 5a6d780

Please sign in to comment.