11package pl .smsapi .proxy ;
22
3+ import org .apache .maven .model .Model ;
4+ import org .apache .maven .model .io .xpp3 .MavenXpp3Reader ;
5+ import org .codehaus .plexus .util .xml .pull .XmlPullParserException ;
36import pl .smsapi .api .authenticationStrategy .AuthenticationStrategy ;
47import pl .smsapi .exception .ProxyException ;
58
1417public class ProxyNative implements Proxy {
1518
1619 private String baseUrl ;
20+ private String userAgent ;
1721
1822 public ProxyNative (String url ) {
1923
@@ -36,7 +40,7 @@ public ProxyNative(String url) {
3640 public String execute (String endpoint , Map <String , String > data , Map <String , InputStream > files , String httpMethod , AuthenticationStrategy authenticationStrategy ) throws Exception {
3741 URL url = createUrl (httpMethod , endpoint , data );
3842 HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
39- connection .setRequestProperty ("User-Agent" , "smsapi-lib/java " + System . getProperty ( "os.name" ));
43+ connection .setRequestProperty ("User-Agent" , generateUserAgent ( ));
4044 connection .setRequestProperty ("Accept" , "*" );
4145 connection .setUseCaches (false );
4246 connection .setDoOutput (true );
@@ -87,6 +91,15 @@ public String execute(String endpoint, Map<String, String> data, Map<String, Inp
8791 return response ;
8892 }
8993
94+ private String generateUserAgent () throws IOException , XmlPullParserException {
95+ if (userAgent == null ) {
96+ MavenXpp3Reader reader = new MavenXpp3Reader ();
97+ Model model = reader .read (new FileReader ("pom.xml" ));
98+ userAgent = "smsapi/java-client:" + model .getVersion () + ";java:" + System .getProperty ("java.vm.version" );
99+ }
100+ return userAgent ;
101+ }
102+
90103 private String readResponsePayload (InputStream inputStream ) throws ProxyException {
91104 BufferedReader inputReader = new BufferedReader (new InputStreamReader (inputStream ));
92105 StringBuilder response = new StringBuilder ();
0 commit comments