NamespaceManager.set(NAMESPACE);
try { //put in a try so that we can have a finally block which resets namespaces
Key key = KeyFactory.createKey("AuthKey", "master");
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
// look for the auth key in the datastore
Entity authEntity = null;
try {
authEntity = datastoreService.get(key);
} catch (EntityNotFoundException e) {
_logger.info("Authentication entity not found");
authEntity = null;
}
if (null != updateAuth) {
//if were updating the auth
if (null == authEntity || !authEntity.getProperty("secret").equals(updateAuth)) {
//if there is no auth key, or the auth key doesn't match the one provided
try {
URLFetchService fetchService = URLFetchServiceFactory.getURLFetchService();
HTTPResponse response = fetchService.fetch(new URL(SECURE_HOST + "/auth/?site=" + req.getHeader("Host") + "&auth=" + updateAuth));
if (response.getResponseCode() == 200) {
//if successfully received a response, save the new auth key
String content = new String(response.getContent());
if (content.equals(new String("OK"))) {
authEntity = new Entity(key);
authEntity.setProperty("secret", updateAuth);
datastoreService.put(authEntity);
}
}
} catch (Exception e) {
_logger.severe("Exception while updating key: " + e);
return null;