Search in sources :

Example 11 with ApiMethod

use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.

the class VisitorAPI method showAllNotaries.

@ApiMethod(name = "searchForNotaries", path = "searchForNotaries", httpMethod = ApiMethod.HttpMethod.POST)
public ArrayList<NotaryGeneralView> showAllNotaries(final User googleUser) throws Exception {
    // ensure authorization
    UserTools.ensureCryptonomicaRegisteredUser(googleUser);
    // 
    List<Notary> notaryList = ofy().load().type(Notary.class).list();
    // 
    ArrayList<NotaryGeneralView> notaryGeneralViewArrayList = new ArrayList<>();
    if (notaryList != null) {
        for (Notary notary : notaryList) {
            Key<CryptonomicaUser> notaryCUkey = Key.create(CryptonomicaUser.class, notary.getId());
            // 
            CryptonomicaUser notaryCryptonomicaUser = ofy().load().key(notaryCUkey).now();
            // 
            UserProfileGeneralView notaryUserProfileGeneralView = new UserProfileGeneralView(notaryCryptonomicaUser);
            // 
            notaryGeneralViewArrayList.add(new NotaryGeneralView(notaryUserProfileGeneralView, notary));
        }
    }
    // 
    LOG.warning("notaryGeneralViewArrayList: " + new Gson().toJson(notaryGeneralViewArrayList));
    // 
    return notaryGeneralViewArrayList;
}
Also used : UserProfileGeneralView(net.cryptonomica.returns.UserProfileGeneralView) ArrayList(java.util.ArrayList) Notary(net.cryptonomica.entities.Notary) Gson(com.google.gson.Gson) NotaryGeneralView(net.cryptonomica.returns.NotaryGeneralView) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 12 with ApiMethod

use of com.google.api.server.spi.config.ApiMethod 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 13 with ApiMethod

use of com.google.api.server.spi.config.ApiMethod 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 14 with ApiMethod

use of com.google.api.server.spi.config.ApiMethod 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)

Example 15 with ApiMethod

use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.

the class EthNodeAPI method ethGetDocBySha256.

// 
/* ---- AddDocRequestObj Class: */
@ApiMethod(name = "getDocBySha256", path = "getDocBySha256", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public // get doc by sha256
Object ethGetDocBySha256(// final HttpServletRequest httpServletRequest,
final User googleUser, final GetDocBySha256Form getDocBySha256Form) throws IllegalArgumentException, UnauthorizedException {
    // ensure registered user ( - may be later only for verificated):
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
    // check form:
    LOG.warning("getDocBySha256Form" + getDocBySha256Form);
    if (getDocBySha256Form == null || getDocBySha256Form.getSha256() == null || getDocBySha256Form.getSha256().length() < 64 || getDocBySha256Form.getSha256().equals("")) {
        throw new IllegalArgumentException("Provided text is to short or empty");
    }
    if (getDocBySha256Form.getSha256() != null && getDocBySha256Form.getSha256().length() > 64) {
        throw new IllegalArgumentException("Provided hash is to long");
    }
    /* ---- Send request to Ethereum node: */
    String ethnodeApiKey = ofy().load().key(Key.create(AppSettings.class, "EthnodeApiKey")).now().getValue();
    String urlAddress = "https://ethnode.cryptonomica.net/api/proofofexistence-get";
    HTTPResponse httpResponse = HttpService.getDocBySha256(urlAddress, "0x" + getDocBySha256Form.getSha256(), ethnodeApiKey);
    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);
    LOG.warning("resObj: " + new Gson().toJson(resObj));
    return resObj;
}
Also used : AppSettings(net.cryptonomica.entities.AppSettings) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) Gson(com.google.gson.Gson) BooleanWrapperObject(net.cryptonomica.returns.BooleanWrapperObject) StringWrapperObject(net.cryptonomica.returns.StringWrapperObject) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Aggregations

ApiMethod (com.google.api.server.spi.config.ApiMethod)54 CryptonomicaUser (net.cryptonomica.entities.CryptonomicaUser)19 Gson (com.google.gson.Gson)16 UserData (com.google.samples.apps.iosched.server.userdata.db.UserData)10 PGPPublicKeyData (net.cryptonomica.entities.PGPPublicKeyData)10 ArrayList (java.util.ArrayList)9 StringWrapperObject (net.cryptonomica.returns.StringWrapperObject)9 NotFoundException (com.google.api.server.spi.response.NotFoundException)8 BadRequestException (com.google.api.server.spi.response.BadRequestException)7 UnauthorizedException (com.google.api.server.spi.response.UnauthorizedException)7 Queue (com.google.appengine.api.taskqueue.Queue)7 HTTPResponse (com.google.appengine.api.urlfetch.HTTPResponse)6 Device (com.google.samples.apps.iosched.server.gcm.db.models.Device)6 MessageSender (com.google.samples.apps.iosched.server.gcm.device.MessageSender)5 AppSettings (net.cryptonomica.entities.AppSettings)5 PGPPublicKeyGeneralView (net.cryptonomica.returns.PGPPublicKeyGeneralView)5 UserProfileGeneralView (net.cryptonomica.returns.UserProfileGeneralView)5 BookmarkedSession (com.google.samples.apps.iosched.server.userdata.db.BookmarkedSession)4 BooleanWrapperObject (net.cryptonomica.returns.BooleanWrapperObject)4 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)4