Search in sources :

Example 6 with CryptonomicaUser

use of net.cryptonomica.entities.CryptonomicaUser in project cryptonomica by Cryptonomica.

the class UserTools method ensureNewCryptonomicaUser.

public static void ensureNewCryptonomicaUser(final User googleUser) throws Exception {
    ensureGoogleAuth(googleUser);
    String userId = googleUser.getUserId();
    Key<CryptonomicaUser> userKey = Key.create(CryptonomicaUser.class, userId);
    CryptonomicaUser cryptonomicaUser = null;
    cryptonomicaUser = ofy().load().key(userKey).now();
    if (cryptonomicaUser != null) {
        throw new Exception("user is already registered");
    }
}
Also used : CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) JSONException(org.json.JSONException) UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException)

Example 7 with CryptonomicaUser

use of net.cryptonomica.entities.CryptonomicaUser in project cryptonomica by Cryptonomica.

the class UserTools method ensureCryptonomicaOfficer.

// end of ensureCryptonomicaRegisteredUser method
/* --- Check if user is an IACC officer: */
public static CryptonomicaUser ensureCryptonomicaOfficer(final User googleUser) throws UnauthorizedException {
    // 
    CryptonomicaUser cryptonomicaUser = ensureCryptonomicaRegisteredUser(googleUser);
    LOG.warning("cryptonomicaUser.getCryptonomicaOfficer(): " + cryptonomicaUser.getCryptonomicaOfficer());
    if (cryptonomicaUser.getCryptonomicaOfficer() == null || !cryptonomicaUser.getCryptonomicaOfficer()) {
        throw new UnauthorizedException("You are not a Cryptonomica officer");
    }
    return cryptonomicaUser;
}
Also used : UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser)

Example 8 with CryptonomicaUser

use of net.cryptonomica.entities.CryptonomicaUser in project cryptonomica by Cryptonomica.

the class ArbitratorsAPI method showAllArbitrators.

@ApiMethod(name = "showAllArbitrators", path = "showAllArbitrators", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public ArrayList<ArbitratorGeneralView> showAllArbitrators(final User googleUser) throws Exception {
    /* --- Ensure authorization: */
    UserTools.ensureCryptonomicaRegisteredUser(googleUser);
    /* --- Load list of all arbitrators: */
    List<Arbitrator> arbitratorsList = ofy().load().type(Arbitrator.class).list();
    /* --- Create an empty list to store arbitrators data  */
    ArrayList<ArbitratorGeneralView> arbitratorGeneralViewArrayList = new ArrayList<>();
    /* ---  fill the arbitratorGeneralViewArrayList */
    if (arbitratorsList != null) {
        for (Arbitrator arbitrator : arbitratorsList) {
            // load CryptonomicaUser data for notary:
            CryptonomicaUser arbitratorCryptonomicaUser = ofy().load().key(Key.create(CryptonomicaUser.class, arbitrator.getId())).now();
            // make ArbitratorGeneralView:
            ArbitratorGeneralView arbitratorGeneralView = new ArbitratorGeneralView(arbitratorCryptonomicaUser, arbitrator);
            // add to ArrayList:
            arbitratorGeneralViewArrayList.add(arbitratorGeneralView);
        }
    }
    // end: if loop
    LOG.warning("arbitratorGeneralViewArrayList: " + new Gson().toJson(arbitratorGeneralViewArrayList));
    return arbitratorGeneralViewArrayList;
}
Also used : ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) Arbitrator(net.cryptonomica.entities.Arbitrator) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) ArbitratorGeneralView(net.cryptonomica.returns.ArbitratorGeneralView) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 9 with CryptonomicaUser

use of net.cryptonomica.entities.CryptonomicaUser in project cryptonomica by Cryptonomica.

the class EthNodeAPI method ethAddDoc.

@ApiMethod(name = "addDoc", path = "addDoc", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public // stores provided document on the Ethereum blockchain
Object ethAddDoc(// final HttpServletRequest httpServletRequest,
final User googleUser, // 
final EthAddDocForm ethAddDocForm) throws IllegalArgumentException, UnauthorizedException {
    // ensure registered user ( - may be later only for verified users):
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
    // check form:
    LOG.warning("ethAddDocForm" + ethAddDocForm + " from " + cryptonomicaUser.getEmail().getEmail());
    if (ethAddDocForm == null || ethAddDocForm.getDocText() == null || ethAddDocForm.getDocText().length() < 12 || ethAddDocForm.getDocText().equals("")) {
        throw new IllegalArgumentException("Provided text is to short or empty");
    }
    if (ethAddDocForm.getDocText() != null && ethAddDocForm.getDocText().length() > 1700) {
        throw new IllegalArgumentException("Provided text is to long");
    }
    /* ---- Send request to Ethereum node: */
    // make request obj:
    EthNodeAPI.AddDocRequestObj addDocRequestObj = new EthNodeAPI.AddDocRequestObj();
    String ethnodeApiKey = ofy().load().key(Key.create(AppSettings.class, "EthnodeApiKey")).now().getValue();
    addDocRequestObj.setApikey(ethnodeApiKey);
    addDocRequestObj.setPublisher(cryptonomicaUser.getEmail().getEmail());
    addDocRequestObj.setText(ethAddDocForm.getDocText());
    String urlAddress = "https://ethnode.cryptonomica.net/api/proofofexistence-add";
    HTTPResponse httpResponse = HttpService.postWithPayload(urlAddress, addDocRequestObj.publisher, addDocRequestObj.text, ethnodeApiKey);
    LOG.warning("httpResponse: " + new Gson().toJson(httpResponse));
    // httpResponse: {"responseCode":200,"headers":[{"name":"Content-Type","value":"application/json"},
    // {"name":"Date","value":"Mon, 11 Jul 2016 00:55:47 GMT"},{"name":"Connection","value":"keep-alive"},
    // {"name":"Content-Length","value":"3448"}],"combinedHeadersMap":{"Content-Type":"application/json","Date":
    // "Mon, 11 Jul 2016 00:55:47 GMT","Connection":"keep-alive","Content-Length":"3448"},
    // "content":[123,34,116,120,72,97,115,104,34,58,34,48,120,99
    // --- valid JSON with headers, and 'content' encoded
    byte[] httpResponseContentBytes = httpResponse.getContent();
    String httpResponseContentString = new String(httpResponseContentBytes, StandardCharsets.UTF_8);
    // Test:
    Object resObj = new Gson().fromJson(httpResponseContentString, Object.class);
    LOG.warning("resObj: " + new Gson().toJson(resObj));
    // resObj: {"txHash":"0xc1897ca491e7dec1537be5935734d863f0c17e3e154f22fa06a7e4d66384b6e2","tx":{"blockHash":"
    // -- valid JSON !!!
    // if success send an email to user:
    final Queue queue = QueueFactory.getDefaultQueue();
    Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
    queue.add(TaskOptions.Builder.withUrl("/_ah/SendGridServlet").param("email", googleUser.getEmail()).param("messageSubject", "You have stored document on blockchain").param("messageText", "Hello! \n\n" + WordUtils.capitalize(cryptonomicaUser.getFirstName()) + " " + WordUtils.capitalize(cryptonomicaUser.getLastName()) + ",\n\n" + "You sent a document to the blockchain!" + "\n\n" + "Document text: " + "\n" + ethAddDocForm.getDocText() + "\n\n" + "with the following result: " + "\n" + httpResponseContentString + "\n\n" + "Best regards, \n\n" + "Cryptonomica team\n\n" + "if you think it's wrong or it is an error, please write to admin@cryptonomica.net \n"));
    // 
    return resObj;
}
Also used : AppSettings(net.cryptonomica.entities.AppSettings) GsonBuilder(com.google.gson.GsonBuilder) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) Gson(com.google.gson.Gson) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) BooleanWrapperObject(net.cryptonomica.returns.BooleanWrapperObject) StringWrapperObject(net.cryptonomica.returns.StringWrapperObject) Queue(com.google.appengine.api.taskqueue.Queue) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 10 with CryptonomicaUser

use of net.cryptonomica.entities.CryptonomicaUser in project cryptonomica by Cryptonomica.

the class EthNodeAPI method requestTestingServlet.

@ApiMethod(name = "requestTestingServlet", path = "requestTestingServlet", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public StringWrapperObject requestTestingServlet(final User googleUser) throws IllegalArgumentException, UnauthorizedException {
    // (!) for Cryptonomica officers only:
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaOfficer(googleUser);
    String urlHost = "https://tomcatweb3j.cryptonomica.net";
    String urlPath = "/TestingServlet";
    String urlAddress = urlHost + urlPath;
    String postRequestBody = "testBodyParameterName=" + "testBodyParameterValue";
    String headerName = "testHeaderName";
    String heaserValue = "testHeaderValue";
    // (!!!!) DO NOT USE API KEY IN TEST REQUEST
    HTTPResponse httpResponse = HttpService.postRequestWithCustomHeader(urlAddress, postRequestBody, headerName, heaserValue);
    // LOG.warning("httpResponse: " + new Gson().toJson(httpResponse));
    byte[] httpResponseContentBytes = httpResponse.getContent();
    String httpResponseContentString = new String(httpResponseContentBytes, StandardCharsets.UTF_8);
    // Test:
    // Object resObj = new Gson().fromJson(httpResponseContentString, Object.class); // --- exception
    // --- works
    TestEntity testEntity = new Gson().fromJson(httpResponseContentString, TestEntity.class);
    LOG.warning("testEntity: " + new Gson().toJson(testEntity));
    // testEntity: {"string":"some string","integer":33,"aBoolean":true,"object":{}} (EthNodeAPI.java:309)
    LOG.warning("testEntity.string: " + testEntity.string);
    LOG.warning("testEntity.integer: " + testEntity.integer);
    LOG.warning("testEntity.aBoolean: " + testEntity.aBoolean);
    LOG.warning("testEntity.object: " + testEntity.object);
    LOG.warning("httpResponseContentString: ");
    LOG.warning(httpResponseContentString);
    // return resObj;
    return new StringWrapperObject(httpResponseContentString);
}
Also used : StringWrapperObject(net.cryptonomica.returns.StringWrapperObject) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) Gson(com.google.gson.Gson) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Aggregations

CryptonomicaUser (net.cryptonomica.entities.CryptonomicaUser)24 ApiMethod (com.google.api.server.spi.config.ApiMethod)19 Gson (com.google.gson.Gson)13 ArrayList (java.util.ArrayList)7 HTTPResponse (com.google.appengine.api.urlfetch.HTTPResponse)6 PGPPublicKeyData (net.cryptonomica.entities.PGPPublicKeyData)6 UnauthorizedException (com.google.api.server.spi.response.UnauthorizedException)5 AppSettings (net.cryptonomica.entities.AppSettings)5 UserProfileGeneralView (net.cryptonomica.returns.UserProfileGeneralView)5 StringWrapperObject (net.cryptonomica.returns.StringWrapperObject)4 GsonBuilder (com.google.gson.GsonBuilder)3 BooleanWrapperObject (net.cryptonomica.returns.BooleanWrapperObject)3 PGPPublicKeyGeneralView (net.cryptonomica.returns.PGPPublicKeyGeneralView)3 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)3 BadRequestException (com.google.api.server.spi.response.BadRequestException)2 NotFoundException (com.google.api.server.spi.response.NotFoundException)2 Queue (com.google.appengine.api.taskqueue.Queue)2 Arbitrator (net.cryptonomica.entities.Arbitrator)2 Verification (net.cryptonomica.entities.Verification)2 ArbitratorGeneralView (net.cryptonomica.returns.ArbitratorGeneralView)2