Skip to content

Commit c11c1aa

Browse files
author
Damian Staszewski
committed
Order in class usages.
1 parent 6fbb6a0 commit c11c1aa

File tree

10 files changed

+206
-64
lines changed

10 files changed

+206
-64
lines changed

src/main/java/com/hirezstudios/HiRezAPI.java

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package pl.stachu540.hirezstudios;
2+
3+
import pl.stachu540.hirezstudios.util.Endpoints;
4+
5+
/**
6+
* @author Damian Staszewski <[email protected]>
7+
* @since 1.8
8+
*/
9+
public class HiRezAPI {
10+
private final String devId;
11+
private final String authKey;
12+
private final Endpoints endpoint;
13+
14+
public HiRezAPI(String devId, String authKey, Endpoints endpoint) {
15+
this.devId = devId;
16+
this.authKey = authKey;
17+
this.endpoint = endpoint;
18+
}
19+
}

src/main/java/com/hirezstudios/games/smite/Language.java renamed to src/main/java/pl/stachu540/hirezstudios/instance/Language.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.hirezstudios.games.smite;
1+
package pl.stachu540.hirezstudios.instance;
22

33
import java.util.HashMap;
44
import java.util.Map;

src/main/java/com/hirezstudios/games/smite/Queue.java renamed to src/main/java/pl/stachu540/hirezstudios/instance/Queue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.hirezstudios.games.smite;
1+
package pl.stachu540.hirezstudios.instance;
22

33
import java.util.HashMap;
44
import java.util.Map;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package pl.stachu540.hirezstudios.instance;
2+
3+
import org.json.JSONObject;
4+
import pl.stachu540.hirezstudios.util.Endpoints;
5+
import pl.stachu540.util.API;
6+
import pl.stachu540.util.MD5Hash;
7+
8+
import java.security.NoSuchAlgorithmException;
9+
import java.text.SimpleDateFormat;
10+
import java.util.Date;
11+
import java.util.SimpleTimeZone;
12+
13+
/**
14+
* @author Damian Staszewski <[email protected]>
15+
* @since 1.8
16+
*/
17+
public class Session {
18+
19+
private final Endpoints endpoints;
20+
private final String devId;
21+
private final String authKey;
22+
private final String timestamp = timeStamp();
23+
24+
private String timeStamp() {
25+
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
26+
sdf.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
27+
return sdf.format(new Date());
28+
}
29+
30+
private final int session_time = 1000 * 60 * 15;
31+
32+
public Session(String devId, String authKey, Endpoints endpoints) {
33+
this.devId = devId;
34+
this.authKey = authKey;
35+
this.endpoints = endpoints;
36+
}
37+
38+
public String getUrl() {
39+
return endpoints.url();
40+
}
41+
42+
public String getTimestamp() { return timestamp; }
43+
44+
public MD5Hash generateSig(String method) throws NoSuchAlgorithmException {
45+
if (method.toLowerCase().endsWith("json")) method = method.substring(0, method.length() - 4);
46+
return new MD5Hash(devId + method + authKey + getTimestamp());
47+
}
48+
49+
public String createSession(MD5Hash signature) throws Exception {
50+
API api = new API(endpoints.url());
51+
String url = "/createsessionJson/" + devId + "/" + signature.getHash() + "/" + timestamp;
52+
JSONObject data = api.getData(API.requestType.GET, url);
53+
JSONObject content = new JSONObject();
54+
if (data.getBoolean("_success")) content = data.getJSONObject("_content");
55+
if (content.getString("ret_msg").equals("Approved")) return content.getString("session_id");
56+
else {
57+
Exception exception = (Exception) Class.forName(data.getString("_exception")).getConstructor(String.class, Throwable.class).newInstance(data.getString("_exceptionMessage"));
58+
throw exception;
59+
}
60+
}
61+
62+
}
63+

src/main/java/com/hirezstudios/games/smite/TierLeauge.java renamed to src/main/java/pl/stachu540/hirezstudios/instance/TierLeauge.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.hirezstudios.games.smite;
1+
package pl.stachu540.hirezstudios.instance;
22

33
import java.util.HashMap;
44
import java.util.Map;
@@ -52,4 +52,8 @@ public enum TierLeauge {
5252
public static TierLeauge valueOf(int id) {
5353
return map.get(id);
5454
}
55+
56+
public final String getName() {
57+
return name().replace("_", " ");
58+
}
5559
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package pl.stachu540.hirezstudios.util;
2+
3+
/**
4+
* @author Damian Staszewski <[email protected]>
5+
* @since 1.8
6+
*/
7+
8+
public enum Endpoints {
9+
Smite_PC("http://api.smitegame.com/smiteapi.svc"),
10+
Smite_XB1("http://api.xbox.smitegame.com/smiteapi.svc"),
11+
Smite_PS4("http://api.ps4.smitegame.com/smiteapi.svc"),
12+
13+
Paladins_PC("http://api.paladins.com/paladinsapi.svc"),
14+
Paladins_XB1("http://api.xbox.paladins.com/paladinsapi.svc"),
15+
Paladins_PS4("http://api.ps4.paladins.com/paladinsapi.svc");
16+
17+
private String url;
18+
19+
Endpoints(String url) {
20+
this.url = url;
21+
}
22+
23+
public String url() { return url; }
24+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package pl.stachu540.hirezstudios.util;
2+
3+
import pl.stachu540.util.MD5Hash;
4+
5+
import java.text.SimpleDateFormat;
6+
import java.util.Date;
7+
import java.util.SimpleTimeZone;
8+
9+
/**
10+
* @author Damian Staszewski <[email protected]>
11+
* @since 1.8
12+
*/
13+
public class Signature {
14+
private final String timestamp = generateTimestamp();
15+
private final String devId;
16+
private final String authKey;
17+
18+
public Signature(String devId, String authKey) {
19+
this.devId = devId;
20+
this.authKey = authKey;
21+
}
22+
23+
private String generateTimestamp() {
24+
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
25+
sdf.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
26+
return sdf.format(new Date());
27+
}
28+
29+
public String getTimestamp() { return timestamp; }
30+
31+
public MD5Hash generateSignature(String method) {
32+
return new MD5Hash(devId + method + authKey + timestamp);
33+
}
34+
}
Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package pl.stachu540;
1+
package pl.stachu540.util;
22

33

44
import org.apache.commons.io.IOUtils;
@@ -43,11 +43,11 @@ public void deleteHeader(String name) {
4343
}
4444

4545
public JSONObject getData(requestType req, String path) {
46-
return getData(req, path, "");
46+
return getData(req, path, null);
4747
}
4848

4949
@SuppressWarnings("UseSpecificCatch")
50-
public JSONObject getData(requestType req, String path, String post) {
50+
public JSONObject getData(requestType req, String path, JSONObject post) {
5151
JSONObject jsonData = new JSONObject("{}");
5252
InputStream is = null;
5353
String content = "";
@@ -62,48 +62,51 @@ public JSONObject getData(requestType req, String path, String post) {
6262
c.setRequestMethod(req.name());
6363
c.setConnectTimeout(timeout);
6464

65-
if (!post.isEmpty()) c.setDoOutput(true);
65+
if (post.length() > 0) c.setDoOutput(true);
6666
c.connect();
67-
if (!post.isEmpty()) {
67+
if (post.length() > 0) {
6868
try (OutputStream o = c.getOutputStream()){
69-
o.write(post.getBytes(Charset.forName("UTF-8")));
69+
o.write(post.toString().getBytes(Charset.forName("UTF-8")));
7070
}
7171
}
7272

7373
is = c.getInputStream();
7474
content = IOUtils.toString(is, c.getContentEncoding());
7575

76-
fillJSONObject(jsonData, true, req.name(), post, url_path, c.getResponseCode(), "", "", content);
76+
fillJSONObject(jsonData, true, req, post, url_path, c.getResponseCode(), null, content);
7777
} catch (Exception e) {
7878
Throwable rootCause = e;
7979
while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
8080
rootCause = rootCause.getCause();
8181
}
82-
83-
fillJSONObject(jsonData, false, req.name(), post, url_path, 0, e.getClass().getSimpleName(), e.getMessage(), content);
84-
System.err.println(e.getMessage());
82+
fillJSONObject(jsonData, false, req, post, url_path, 0, e, content);
83+
e.printStackTrace();
8584
} finally {
8685
if (is != null) {
8786
try {
8887
is.close();
8988
} catch (Exception e) {
90-
fillJSONObject(jsonData, false, req.name(), post, url_path, 0, e.getClass().getName(), e.getMessage(), content);
91-
System.err.println(e.getMessage());
89+
fillJSONObject(jsonData, false, req, post, url_path, 0, e, content);
90+
e.printStackTrace();
9291
}
9392
}
9493
}
9594

9695
return jsonData;
9796
}
9897

99-
private void fillJSONObject(JSONObject jsonObject, boolean success, String type, String post, String url, int responseCode, String exception, String exceptionMessage, String jsonContent) {
100-
jsonObject.put("_success", success);
101-
jsonObject.put("_type", type);
102-
jsonObject.put("_post", post);
103-
jsonObject.put("_url", url);
104-
jsonObject.put("_http", responseCode);
105-
jsonObject.put("_exception", exception);
106-
jsonObject.put("_exceptionMessage", exceptionMessage);
107-
jsonObject.put("_content", jsonContent);
98+
private void fillJSONObject(JSONObject jsonObject, boolean success, requestType type, JSONObject post, String url, int responseCode, Exception exception, String jsonContent) {
99+
100+
JSONObject data = new JSONObject("{}");
101+
data.put("status", success);
102+
data.put("requestType", type.name());
103+
data.put("url", url);
104+
data.put("postData", post);
105+
data.put("responseCode", responseCode);
106+
107+
jsonObject.put("data", data);
108+
jsonObject.put("content", new JSONObject(jsonContent));
109+
jsonObject.put("exception", exception);
110+
108111
}
109112
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package pl.stachu540.util;
2+
3+
import java.security.MessageDigest;
4+
5+
/**
6+
* @author Damian Staszewski <[email protected]>
7+
* @since 1.8
8+
*/
9+
public class MD5Hash {
10+
11+
private final String data;
12+
private final StringBuilder hash = new StringBuilder();
13+
14+
public MD5Hash(String data) {
15+
this.data = data;
16+
try {
17+
MessageDigest md = MessageDigest.getInstance("MD5");
18+
md.update(data.getBytes());
19+
20+
byte byteData[] = md.digest();
21+
22+
for (int i = 0; i < byteData.length; i++) {
23+
String hex = Integer.toHexString(0xff & byteData[i]);
24+
if (hex.length() == 1) hash.append("0");
25+
hash.append(hex);
26+
}
27+
} catch (Exception e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
32+
public String getText() { return data; }
33+
34+
public String getHash() { return hash.toString(); }
35+
}

0 commit comments

Comments
 (0)