All Downloads are FREE. Search and download functionalities are using the official Maven repository.

base.jee.api.cassandra.UpsertTemplateResource Maven / Gradle / Ivy

/*
 This is free and unencumbered software released into the public domain.

 Anyone is free to copy, modify, publish, use, compile, sell, or
 distribute this software, either in source code form or as a compiled
 binary, for any purpose, commercial or non-commercial, and by any
 means.

 In jurisdictions that recognize copyright laws, the author or authors
 of this software dedicate any and all copyright interest in the
 software to the public domain. We make this dedication for the benefit
 of the public at large and to the detriment of our heirs and
 successors. We intend this dedication to be an overt act of
 relinquishment in perpetuity of all present and future rights to this
 software under copyright law.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
package base.jee.api.cassandra;

import java.io.IOException;

import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;

import java.nio.ByteBuffer;
import java.util.Arrays;

import base.Command;
import base.jee.Constants;
import base.json.Json;
import base.security.ResourceUid;
import base.security.User;
import base.text.Base64Encode;

import static base.jee.api.cassandra.util.InsertUpdateNotification.insertUpdateNotification;
import static base.jee.api.cassandra.util.Log.log;

public class UpsertTemplateResource extends Command {

	private CassandraAPI c;
	private User user;
	private String template;
	private String name;
	private byte[] content;
	private boolean binary;

	public UpsertTemplateResource(CassandraAPI c, User user, String template, String name, byte[] content, boolean binary) {
		this.c = c;
		this.user = user;
		this.template = template;
		this.name = name;
		this.content = content;
		this.binary = binary;

		if(c == null) {
			throw new IllegalArgumentException("Invalid parameter: c");
		}
		if(user == null || !user.isAuthenticated()) {
			throw new IllegalArgumentException("Invalid parameter: user");
		}
		if(template == null || template.length() == 0) {
			throw new IllegalArgumentException("Invalid parameter: template");
		}
		if(template.contains(" ")) {
			throw new IllegalArgumentException("template name must not contain whitespace");
		}
		if(name == null || name.length() == 0) {
			throw new IllegalArgumentException("Invalid parameter: name");
		}
		if(name.contains(" ")) {
			throw new IllegalArgumentException("resource name must not contain whitespace");
		}
		if(content == null || content.length == 0) {
			throw new IllegalArgumentException("Invalid parameter: content");
		}
	}

	@Override
	protected void execute() throws IOException {
		Session s = c.getCassandraSession();

		if(!user.hasRole(Constants.TEMPLATE_MANAGE_ROLE)) {
			log(s, "WARN", user, "Permission denied invoking: " + UpsertTemplateResource.class.getSimpleName() + " " + getJsonParameters());
			throw new IllegalStateException("You do not have permission to manage template resources.");
		}

		PreparedStatement p = s.prepare("select name from template where site=? and name=?");
		ResultSet rs = s.execute(p.bind(user.getSite(), template));

		if(!rs.iterator().hasNext()) {
			throw new IllegalArgumentException("Unknown template: " + template);
		}

		p = s.prepare("select content from template_resource where site=? and template=? and resource=?");
		rs = s.execute(p.bind(user.getSite(), template, name));
		for(Row r : rs) {
			if(!Arrays.equals(r.getBytes(0).array(), content)) {
				p = s.prepare("update template_resource set content=? where site=? and template=? and resource=?");
				s.execute(p.bind(ByteBuffer.wrap(content), user.getSite(), template, name));
				log(s, "INFO", user, "Updated custom template resource {\"template\":\"" + Json.escape(template) + "\",\"resource\":\"" + Json.escape(name) + "\"}", new ResourceUid("Template", name));
				insertUpdateNotification(s, user, "template", "update", template + "|" + name);
			} else {
				log(s, "FINEST", user, "Save to custom template resource does not change content. {\"template\":\"" + Json.escape(template) + "\",\"resource\":\"" + Json.escape(name) + "\"}", new ResourceUid("Template", name));
			}
			return;
		}

		p = s.prepare("insert into template_resource (site,template,resource,content,isbinary) values(?,?,?,?,?)");
		s.execute(p.bind(user.getSite(), template, name, ByteBuffer.wrap(content), binary));
		log(s, "INFO", user, "Added custom template resource {\"template\":\"" + Json.escape(template) + "\",\"resource\":\"" + Json.escape(name) + "\"}", new ResourceUid("Template", name));
		insertUpdateNotification(s, user, "template", "add", template + "|" + name);
	}

	@Override
	public String getJsonParameters() {
		return "{" +
				"\"template\":\"" + Json.escape(template)+ "\"," +
				"\"name\":\"" + Json.escape(name)+ "\"," +
				"\"content\":\"" + Base64Encode.encode(content) + "\"" +
				"}";
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy