Created
January 24, 2017 11:10
-
-
Save sjehutch/86f71803a440cc513a22e15b31232de2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.provagroup.wamp.client; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import com.provagroup.provaserver.wamp.ServerResponseMessage; | |
import com.provagroup.provaserver.wamp.UploadFile; | |
import com.provagroup.provaserver.wamp.WampApiFieldKeys; | |
public class LegitWampClient { | |
private static final List<Map<String, Object>> EMPTY_LIST = new ArrayList<>(); | |
private final String user; | |
private final String pw; | |
private final String uri; | |
private final String realm; | |
private PgWampClient client; | |
private static final List<String> USER_ARGS; | |
static { | |
List<String> list = new ArrayList<>(); | |
list.add(WampApiFieldKeys.U_ID); | |
list.add(WampApiFieldKeys.U_LAST); | |
list.add(WampApiFieldKeys.U_FIRST); | |
list.add(WampApiFieldKeys.U_MIDDLE); | |
list.add(WampApiFieldKeys.U_TITLE); | |
list.add(WampApiFieldKeys.U_SUFFIX); | |
list.add(WampApiFieldKeys.U_COVER_IMG); | |
list.add(WampApiFieldKeys.U_PROFILE_IMG); | |
list.add(WampApiFieldKeys.U_ADDRESS_LIST); | |
USER_ARGS = Collections.unmodifiableList(list ); | |
} | |
public LegitWampClient(String uri, String realm, String user, String pw) { | |
this.uri = uri; | |
this.realm = realm; | |
this.user = user; | |
this.pw = pw; | |
} | |
private PgWampClient getClient() { | |
if (client == null) { | |
client = PgWampClient.getClient(uri, realm, user, pw); | |
} | |
return client; | |
} | |
public String getRealm() { | |
return realm; | |
} | |
public static LegitWampClient getClient(String uri, String realm, String user, String pw) { | |
return new LegitWampClient(uri, realm, user, pw); | |
} | |
@SuppressWarnings("unchecked") | |
public String forgotPassword(String email) { | |
final PgWampClient client = getGuestClient("<SERVER_ADDRESS_METHOD>"); | |
String response = "Failed forgot password request"; | |
try { | |
List<Map<String, Object>> args = new ArrayList<>(); | |
Map<String, Object> m = new HashMap<>(); | |
m.put(WampApiFieldKeys.U_EMAIL, email); | |
args.add(m); | |
List<Object> ret = client.sendRequest("forgotPassword", args ); | |
if (ret != null && ret.size() == 1) { | |
response = (String) ((Map<String, Object>)ret.get(0)).get(WampApiFieldKeys.FP_RES); | |
} | |
return response; | |
} finally { | |
if (client != null) { | |
client.close(); | |
} | |
} | |
} | |
private PgWampClient getGuestClient(String realm) { | |
return PgWampClient.getClient(uri, realm, "<EMAIL>","<KEY>"); | |
} | |
public Map<String, Object> getLoginUser() { | |
List<Map<String, Object>> args = new ArrayList<>(); | |
List<Object> ret = getClient().sendRequest("getLoginUser", args); | |
@SuppressWarnings("unchecked") | |
Map<String, Object> user = (Map<String, Object>) ret.get(0); | |
return user; | |
} | |
public void login() { | |
getClient(); | |
} | |
public List<Object> getMyCollection() { | |
PgWampClient c = getClient(); | |
List<Object> list = c.sendRequest("myAssets", EMPTY_LIST); | |
return list; | |
} | |
public void updatePassword(String uid, String pw2, String newpw, String newpw2) { | |
if (newpw != null && newpw2 != null && newpw.compareTo(newpw2) == 0) { | |
PgWampClient c = getClient(); | |
List<Map<String, Object>> args = new ArrayList<>(); | |
Map<String, Object> u = new HashMap<String, Object>(); | |
u.put(WampApiFieldKeys.U_ID, uid); | |
u.put(WampApiFieldKeys.U_PWOLD, pw2); | |
u.put(WampApiFieldKeys.U_PWNEW, newpw); | |
args.add(u); | |
c.sendRequest("setLoginPassword", args); | |
} else { | |
throw new IllegalArgumentException("Passwords must match"); | |
} | |
} | |
public Map<String, Object> getAsset(String serno) { | |
Map<String, Object> a = new HashMap<String, Object>(); | |
a.put(WampApiFieldKeys.ASSET_SERIAL_NO, serno); | |
return getAsset(a); | |
} | |
public Map<String, Object> getAssetByTagid(String tagid) { | |
Map<String, Object> a = new HashMap<String, Object>(); | |
a.put(WampApiFieldKeys.ASSET_TAG_ID, tagid); | |
return getAsset(a); | |
} | |
private Map<String, Object> getAsset(Map<String, Object> args) { | |
PgWampClient c = getClient(); | |
List<Map<String, Object>> list = new ArrayList<>(); | |
list.add(args); | |
List<Object> ret = c.sendRequest("getAsset", list); | |
@SuppressWarnings("unchecked") | |
Map<String, Object> asset = (Map<String, Object>) ret.get(0); | |
return asset; | |
} | |
public Map<String, Object> getError(Map<String, Object> args) { | |
PgWampClient c = getClient(); | |
List<Map<String, Object>> list = new ArrayList<>(); | |
list.add(args); | |
List<Object> ret = c.sendRequest("getSilly", list); | |
@SuppressWarnings("unchecked") | |
Map<String, Object> asset = (Map<String, Object>) ret.get(0); | |
return asset; | |
} | |
public Map<String, Object> registerAsset(String serno, String certserno) { | |
PgWampClient c = getClient(); | |
List<Map<String, Object>> args = new ArrayList<>(); | |
Map<String, Object> a = new HashMap<String, Object>(); | |
a.put(WampApiFieldKeys.ASSET_SERIAL_NO, serno); | |
a.put(WampApiFieldKeys.CERT_SERIAL_NO, certserno); | |
args.add(a); | |
List<Object> ret = c.sendRequest("regAsset", args); | |
@SuppressWarnings("unchecked") | |
Map<String, Object> asset = (Map<String, Object>) ret.get(0); | |
return asset; | |
} | |
public Map<String,Object> updateUser(Map<String, Object> user) { | |
PgWampClient c = getClient(); | |
List<Map<String, Object>> args = new ArrayList<>(); | |
Map<String, Object> m = new HashMap<>(); | |
for (String key : USER_ARGS) { | |
add(m, key, user); | |
} | |
args.add(m); | |
List<Object> ret = c.sendRequest("updateLoginUser", args); | |
@SuppressWarnings("unchecked") | |
Map<String, Object> retUser = (Map<String, Object>) ret.get(0); | |
return retUser; | |
} | |
private void add(Map<String, Object> m, String key, Map<String, Object> user) { | |
Object val = user.get(key); | |
if (val != null) { | |
m.put(key, val); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
public Map<String, Object> registerUser(Map<String, Object> args) { | |
PgWampClient c = getGuestClient("<SERVER_ADDRESS>"); | |
try { | |
List<Map<String, Object>> o = new ArrayList<>(); | |
o.add(args); | |
return (Map<String, Object>) ((List<Object>) c.sendRequest("registerUser", o)).get(0); | |
} finally { | |
c.close(); | |
} | |
} | |
public String getClientUser() { | |
return user; | |
} | |
public Map<String, Object> uploadMedia(Map<String, Object> args, File sourceFile) { | |
PgWampClient c = getClient(); | |
List<Map<String, Object>> list = new ArrayList<>(); | |
list.add(args); | |
List<Object> ret = c.sendRequest("mediaUpload", list); | |
@SuppressWarnings("unchecked") | |
Map<String, Object> upload = (Map<String, Object>) ret.get(0); | |
String url = (String) upload.get(WampApiFieldKeys.MEDIA_UPLOAD_URL); | |
String name = (String) upload.get(WampApiFieldKeys.MEDIA_FILE_NAME); | |
// TODO: decide on error response handling for the client. | |
ServerResponseMessage resp = UploadFile.uploadFile(sourceFile, url, name); | |
return upload; | |
} | |
public Map<String, Object> updateAsset(Map<String, Object> a) { | |
PgWampClient c = getClient(); | |
List<Map<String, Object>> args = new ArrayList<>(); | |
args.add(a); | |
List<Object> ret = c.sendRequest("updateAssetImages", args); | |
@SuppressWarnings("unchecked") | |
Map<String, Object> retUser = (Map<String, Object>) ret.get(0); | |
return retUser; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment