Search in sources :

Example 16 with CryptonomicaUser

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

the class VerificationAPI method getVerificationByWebSafeString.

// end: getVerificationByID
/* --- Get verification info by web-safe key string: */
@ApiMethod(name = "getVerificationByWebSafeString", path = "getVerificationByWebSafeString", httpMethod = ApiMethod.HttpMethod.GET)
@SuppressWarnings("unused")
public VerificationGeneralView getVerificationByWebSafeString(final HttpServletRequest httpServletRequest, final User googleUser, @Named("verificationWebSafeString") final String verificationWebSafeString) throws // see: https://cloud.google.com/appengine/docs/java/endpoints/exceptions
UnauthorizedException, BadRequestException, NotFoundException {
    /* --- Check authorization: */
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
    /* --- Check input: */
    if (verificationWebSafeString == null || verificationWebSafeString.equals("")) {
        throw new BadRequestException("Verification ID missing");
    }
    /* --- Load verification entity from DS: */
    Key<Verification> verificationKey = Key.create(verificationWebSafeString);
    Verification verification = ofy().load().key(verificationKey).now();
    if (verification == null) {
        throw new NotFoundException("Verification info not found");
    }
    /* --- Create new verification info representation: */
    VerificationGeneralView verificationGeneralView = new VerificationGeneralView(verification);
    LOG.warning(new Gson().toJson(verificationGeneralView));
    return verificationGeneralView;
}
Also used : BadRequestException(com.google.api.server.spi.response.BadRequestException) NotFoundException(com.google.api.server.spi.response.NotFoundException) Gson(com.google.gson.Gson) Verification(net.cryptonomica.entities.Verification) VerificationGeneralView(net.cryptonomica.returns.VerificationGeneralView) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 17 with CryptonomicaUser

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

the class UserTools method ensureNotaryOrCryptonomicaOfficer.

// end of ensureCryptonomicaOfficer method
/* --- Check if user is a notary or IACC officer: */
public static CryptonomicaUser ensureNotaryOrCryptonomicaOfficer(final User googleUser) throws UnauthorizedException {
    // 
    CryptonomicaUser cryptonomicaUser = ensureCryptonomicaRegisteredUser(googleUser);
    // 
    LOG.warning("cryptonomicaUser: ");
    LOG.warning(new Gson().toJson(cryptonomicaUser));
    // if (cryptonomicaOfficer == null && notary == null) {
    LOG.warning("cryptonomicaUser.getCryptonomicaOfficer(): " + cryptonomicaUser.getCryptonomicaOfficer());
    LOG.warning("cryptonomicaUser.getNotary(): " + cryptonomicaUser.getNotary());
    // if isCryptonomicaOfficer and isNotary are both false or null:
    if ((cryptonomicaUser.getCryptonomicaOfficer() == null || !cryptonomicaUser.getCryptonomicaOfficer()) && (cryptonomicaUser.getNotary() == null || !cryptonomicaUser.getNotary())) {
        throw new UnauthorizedException("You are not a Notary or Cryptonomica officer");
    }
    return cryptonomicaUser;
}
Also used : UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) Gson(com.google.gson.Gson) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser)

Example 18 with CryptonomicaUser

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

the class UserTools method ensureCryptonomicaRegisteredUser.

/* --- Check if user is registered user: */
public static CryptonomicaUser ensureCryptonomicaRegisteredUser(final User googleUser) throws UnauthorizedException {
    ensureGoogleAuth(googleUser);
    CryptonomicaUser cryptonomicaUser = null;
    try {
        cryptonomicaUser = ofy().load().key(Key.create(CryptonomicaUser.class, googleUser.getUserId())).now();
    } catch (Exception e) {
        LOG.warning(e.getMessage());
    }
    if (cryptonomicaUser == null) {
        throw new UnauthorizedException("You are not registered on Cryptonomica server");
    }
    return cryptonomicaUser;
}
Also used : UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) JSONException(org.json.JSONException) UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException)

Example 19 with CryptonomicaUser

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

the class CSUploadHandlerServlet method doPost.

@Override
public void doPost(// User googleUser, // <<< -- not legal here
HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    // ensure registered user // --- NOT WORK with CS Upload Handler Servlet
    // UserService userService = UserServiceFactory.getUserService();
    // User googleUser = userService.getCurrentUser();
    // -----------------
    LOG.warning("[Headers values]: ");
    LOG.warning("origin: " + req.getHeader("origin"));
    LOG.warning("Referer: " + req.getHeader("Referer"));
    LOG.warning("Cookie: " + req.getHeader("Cookie"));
    LOG.warning("X-AppEngine-BlobUpload: " + req.getHeader("X-AppEngine-BlobUpload"));
    LOG.warning("imageUploadKey: " + req.getHeader("imageUploadKey"));
    LOG.warning("[Additional: ]");
    LOG.warning("req.getQueryString(): " + req.getQueryString());
    LOG.warning("req.getRemoteUser()" + req.getRemoteUser());
    // -----------------------------------------------------------------
    // -----------------------------------------------------------------
    String imageUploadKey = req.getHeader("imageUploadKey");
    if (imageUploadKey == null) {
        throw new ServletException("imageUploadKey == null");
    }
    List<CryptonomicaUser> cryptonomicaUserList = ofy().load().type(CryptonomicaUser.class).filter("imageUploadKey", imageUploadKey).list();
    if (cryptonomicaUserList.size() > 1) {
        LOG.warning("Get a list with more than one Cryptonomica user: ");
        for (CryptonomicaUser cryptonomicaUser : cryptonomicaUserList) {
            LOG.warning(cryptonomicaUser.getEmail().getEmail());
        }
        throw new ServletException("Get a list with more than one Cryptonomica user");
    }
    if (cryptonomicaUserList.isEmpty()) {
        LOG.warning("cryptonomicaUserList is empty");
        throw new ServletException("cryptonomicaUserList is empty");
    }
    CryptonomicaUser cryptonomicaUser = cryptonomicaUserList.get(0);
    // Returns the FileInfo for any files that were uploaded, keyed by the upload form "name" field.
    // This method should only be called from within a request served by the destination of a createUploadUrl call.
    // https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/blobstore/BlobstoreService#getFileInfos-HttpServletRequest-
    // 
    java.util.Map<java.lang.String, java.util.List<FileInfo>> fileInfoListsMap = BlobstoreServiceFactory.getBlobstoreService().getFileInfos(req);
    LOG.warning("[LOGGER]: " + new Gson().toJson(fileInfoListsMap));
    ArrayList<UploadedFileData> uploadedFilesDataList = new ArrayList<>();
    for (java.util.List<FileInfo> fileInfoList : fileInfoListsMap.values()) {
        for (FileInfo fileInfo : fileInfoList) {
            UploadedFileData uploadedFileData = new UploadedFileData();
            uploadedFileData.fileInfo = fileInfo;
            LOG.warning("uploadedFileData created:" + new Gson().toJson(uploadedFileData));
            BlobKey blobKey = BlobstoreServiceFactory.getBlobstoreService().createGsBlobKey(fileInfo.getGsObjectName());
            uploadedFileData.BlobKey = blobKey.getKeyString();
            uploadedFileData.fileServeServletLink = HOST + "/serve?blob-key=" + blobKey.getKeyString();
            // works only for images (PNG, JPEG, GIF, TIFF, BMP, ICO, WEBP)
            for (com.google.appengine.api.images.Image.Format type : com.google.appengine.api.images.Image.Format.values()) {
                LOG.warning("com.google.appengine.api.images.Image.Format type: " + type.toString());
                LOG.warning("fileInfo.getContentType(): " + fileInfo.getContentType());
                if (fileInfo.getContentType().toLowerCase().contains(type.toString().toLowerCase())) {
                    uploadedFileData.servingUrlFromgsObjectName = ImagesServiceFactory.getImagesService().getServingUrl(ServingUrlOptions.Builder.withGoogleStorageFileName(fileInfo.getGsObjectName()));
                    // should be the same as servingUrlFromGsBlobKey
                    uploadedFileData.servingUrlFromGsBlobKey = ImagesServiceFactory.getImagesService().getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey));
                // should be the same as servingUrlFromgsObjectName
                }
            }
            // end for ( type : .values())
            uploadedFilesDataList.add(uploadedFileData);
        }
    // end for (fileInfo : fileInfoList)
    }
    // end for (fileInfoList : fileInfoListsMap.values())
    Link oldImageLink = cryptonomicaUser.getUserCurrentImageLink();
    if (oldImageLink != null) {
        if (cryptonomicaUser.getOldUserImageLinks() == null) {
            cryptonomicaUser.setOldUserImageLinks(new ArrayList<Link>());
        }
        cryptonomicaUser.getOldUserImageLinks().add(oldImageLink);
    }
    cryptonomicaUser.setUserCurrentImageLink(new Link(uploadedFilesDataList.get(0).servingUrlFromgsObjectName.replace("http:", "https:")));
    cryptonomicaUser.setImageUploadKeyToNull();
    ofy().save().entity(cryptonomicaUser).now();
    // 
    ImageData imageData = new ImageData(Key.create(CryptonomicaUser.class, cryptonomicaUser.getUserId()), uploadedFilesDataList.get(0).BlobKey, uploadedFilesDataList.get(0).fileInfo, uploadedFilesDataList.get(0).fileServeServletLink, uploadedFilesDataList.get(0).servingUrlFromgsObjectName, uploadedFilesDataList.get(0).fileInfo.getGsObjectName());
    ofy().save().entity(imageData).now();
    // 
    res.setContentType("application/json");
    res.setCharacterEncoding("UTF-8");
    res.addHeader("Access-Control-Allow-Origin", "*");
    // get the stream to write the data
    PrintWriter pw = res.getWriter();
    Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    pw.println(gson.toJson(uploadedFilesDataList));
    LOG.warning("uploadedFilesDataMap" + new Gson().toJson(uploadedFilesDataList));
    // closing the stream
    pw.close();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) ServletException(javax.servlet.ServletException) BlobKey(com.google.appengine.api.blobstore.BlobKey) FileInfo(com.google.appengine.api.blobstore.FileInfo) ImageData(net.cryptonomica.entities.ImageData) ArrayList(java.util.ArrayList) List(java.util.List) Link(com.google.appengine.api.datastore.Link) PrintWriter(java.io.PrintWriter)

Example 20 with CryptonomicaUser

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

the class ArbitratorsAPI method addArbitrator.

@ApiMethod(name = "addArbitrator", path = "addArbitrator", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public ArbitratorGeneralView addArbitrator(final User googleUser, final AddArbitratorForm addArbitratorForm) throws Exception {
    /* --- Ensure cryptonomica officer: */
    UserTools.ensureCryptonomicaOfficer(googleUser);
    /* --- Check form: */
    if (addArbitratorForm.getId() == null) {
        throw new IllegalArgumentException("User ID is missing");
    }
    // create arbitrator from form:
    Arbitrator arbitrator = new Arbitrator(addArbitratorForm);
    // save arbitrator in DS:
    Key<Arbitrator> arbitratorKey = ofy().save().entity(arbitrator).now();
    // add info to CryptonomicaUser
    CryptonomicaUser arbitratorCryptonomicaUser = ofy().load().key(Key.create(CryptonomicaUser.class, arbitrator.getId())).now();
    arbitratorCryptonomicaUser.setArbitrator(Boolean.TRUE);
    ofy().save().entity(arbitratorCryptonomicaUser).now();
    // create ArbitratorGeneralView
    ArbitratorGeneralView arbitratorGeneralView = new ArbitratorGeneralView(arbitratorCryptonomicaUser, arbitrator);
    LOG.warning("new arbitrator created: " + new Gson().toJson(arbitratorGeneralView));
    return arbitratorGeneralView;
}
Also used : 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)

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