Java tutorial
/** * Copyright 2013 Gitana Software, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * For more information, please contact Gitana Software, Inc. at this * address: * * info@gitanasoftware.com */ package org.gitana.platform.client.stack; import com.fasterxml.jackson.databind.node.ObjectNode; import org.gitana.http.HttpPayload; import org.gitana.platform.client.attachment.Attachment; import org.gitana.platform.client.beans.ACL; import org.gitana.platform.client.datastore.DataStore; import org.gitana.platform.client.log.LogEntry; import org.gitana.platform.client.platform.AbstractPlatformDocumentImpl; import org.gitana.platform.client.platform.Platform; import org.gitana.platform.client.platform.PlatformDataStore; import org.gitana.platform.client.principal.DomainPrincipal; import org.gitana.platform.client.support.Response; import org.gitana.platform.client.team.Team; import org.gitana.platform.client.util.DriverUtil; import org.gitana.platform.services.authority.AuthorityGrant; import org.gitana.platform.support.Pagination; import org.gitana.platform.support.ResultMap; import org.gitana.platform.support.TypedIDConstants; import org.gitana.util.JsonUtil; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author uzi */ public class StackImpl extends AbstractPlatformDocumentImpl implements Stack { public StackImpl(Platform platform, ObjectNode obj, boolean isSaved) { super(platform, obj, isSaved); } @Override public String getTypeId() { return TypedIDConstants.TYPE_STACK; } @Override protected String getResourceUri() { return "/stacks/" + getId(); } @Override public String getKey() { return getString(FIELD_KEY); } @Override public void setKey(String key) { set(FIELD_KEY, key); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// // // SELF // ///////////////////////////////////////////////////////////////////////////////////////////////////////// @Override public void reload() { Stack project = getPlatform().readStack(this.getId()); this.reload(project.getObject()); } @Override public void update() { getRemote().put(getResourceUri(), getObject()); } @Override public void delete() { getRemote().delete(getResourceUri()); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// // // ACL // ///////////////////////////////////////////////////////////////////////////////////////////////////////// @Override public ACL getACL() { Response response = getRemote().get(getResourceUri() + "/acl/list"); return DriverUtil.toACL(response); } @Override public List<String> getACL(String principalId) { Response response = getRemote().get(getResourceUri() + "/acl?id=" + principalId); return DriverUtil.toStringList(response); } @Override public void grant(String principalId, String authorityId) { getRemote().post(getResourceUri() + "/authorities/" + authorityId + "/grant?id=" + principalId); } @Override public void grant(DomainPrincipal principal, String authorityId) { grant(principal.getDomainQualifiedId(), authorityId); } @Override public void revoke(String principalId, String authorityId) { getRemote().post(getResourceUri() + "/authorities/" + authorityId + "/revoke?id=" + principalId); } @Override public void revoke(DomainPrincipal principal, String authorityId) { revoke(principal.getDomainQualifiedId(), authorityId); } @Override public void revokeAll(String principalId) { revoke(principalId, "all"); } @Override public void revokeAll(DomainPrincipal principal) { revokeAll(principal.getDomainQualifiedId()); } @Override public boolean hasAuthority(String principalId, String authorityId) { boolean has = false; Response response = getRemote() .post(getResourceUri() + "/authorities/" + authorityId + "/check?id=" + principalId); if (response.getObjectNode().has("check")) { has = response.getObjectNode().get("check").booleanValue(); } return has; } @Override public boolean hasAuthority(DomainPrincipal principal, String authorityId) { return hasAuthority(principal.getDomainQualifiedId(), authorityId); } @Override public Map<String, Map<String, AuthorityGrant>> getAuthorityGrants(List<String> principalIds) { ObjectNode object = JsonUtil.createObject(); JsonUtil.objectPut(object, "principals", principalIds); Response response = getRemote().post(getResourceUri() + "/authorities", object); return getFactory().principalAuthorityGrants(response); } @Override public boolean hasPermission(String principalId, String permissionId) { boolean has = false; Response response = getRemote() .post(getResourceUri() + "/permissions/" + permissionId + "/check?id=" + principalId); if (response.getObjectNode().has("check")) { has = response.getObjectNode().get("check").booleanValue(); } return has; } @Override public boolean hasPermission(DomainPrincipal principal, String authorityId) { return hasPermission(principal.getDomainQualifiedId(), authorityId); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// // // TEAMS // ///////////////////////////////////////////////////////////////////////////////////////////////////////// @Override public Team readTeam(String teamKey) { Team team = null; try { Response response = getRemote().get(getResourceUri() + "/teams/" + teamKey); team = getFactory().team(getCluster(), this, teamKey, response); } catch (Exception ex) { // swallow for the time being // TODO: the remote layer needs to hand back more interesting more interesting // TODO: information so that we can detect a proper 404 } return team; } @Override public ResultMap<Team> listTeams() { Map<String, String> params = DriverUtil.params(); Response response = getRemote().get(getResourceUri() + "/teams", params); return getFactory().teams(getCluster(), this, response); } @Override public Team createTeam(String teamKey) { return createTeam(teamKey, null); } @Override public Team createTeam(String teamKey, ObjectNode object) { // allow for null object if (object == null) { object = JsonUtil.createObject(); } getRemote().post(getResourceUri() + "/teams?key=" + teamKey, object); return readTeam(teamKey); } @Override public void deleteTeam(String teamKey) { getRemote().delete(getResourceUri() + "/teams/" + teamKey); } @Override public String getTeamableBaseUri() { return getResourceUri(); } @Override public Team getOwnersTeam() { return this.readTeam("owners"); } ///////////////////////////////////////////////////////////////////////////////////////////////////////// // // ATTACHMENTS // ///////////////////////////////////////////////////////////////////////////////////////////////////////// @Override public void uploadAttachment(byte[] bytes, String contentType) { uploadAttachment("default", bytes, contentType); } @Override public void uploadAttachment(String attachmentId, byte[] bytes, String contentType) { // build the uri String uri = getResourceUri() + "/attachments/" + attachmentId; try { getRemote().upload(uri, bytes, contentType); } catch (Exception ex) { throw new RuntimeException(ex); } } @Override public void uploadAttachment(String attachmentId, byte[] bytes, String contentType, String fileName) { // build the uri String uri = getResourceUri() + "/attachments/" + attachmentId; try { getRemote().upload(uri, bytes, contentType, fileName); } catch (Exception ex) { throw new RuntimeException(ex); } } @Override public void uploadAttachments(HttpPayload... payloads) { Map<String, String> params = new HashMap<String, String>(); uploadAttachments(params, payloads); } @Override public void uploadAttachments(Map<String, String> params, HttpPayload... payloads) { // build the uri String uri = getResourceUri() + "/attachments"; try { getRemote().upload(uri, params, payloads); } catch (Exception ex) { throw new RuntimeException(ex); } } @Override public byte[] downloadAttachment() { return downloadAttachment("default"); } @Override public byte[] downloadAttachment(String attachmentId) { // build the uri String uri = getResourceUri() + "/attachments/" + attachmentId; byte[] bytes = null; try { bytes = getRemote().downloadBytes(uri); } catch (Exception ex) { throw new RuntimeException(ex); } return bytes; } @Override public ResultMap<Attachment> listAttachments() { // build the uri String uri = getResourceUri() + "/attachments"; Response response = getRemote().get(uri); return getFactory().attachments(this, response); } @Override public String getDownloadUri(String attachmentId) { return getResourceUri() + "/attachments/" + attachmentId; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// // // LOG ENTRIES // ///////////////////////////////////////////////////////////////////////////////////////////////////////// @Override public ResultMap<LogEntry> listLogEntries() { return listLogEntries(null); } @Override public ResultMap<LogEntry> listLogEntries(Pagination pagination) { Map<String, String> params = DriverUtil.params(pagination); Response response = getRemote().get(getResourceUri() + "/logs", params); return getFactory().logEntries(getCluster(), response); } @Override public ResultMap<LogEntry> queryLogEntries(ObjectNode query) { return queryLogEntries(query, null); } @Override public ResultMap<LogEntry> queryLogEntries(ObjectNode query, Pagination pagination) { Map<String, String> params = DriverUtil.params(pagination); Response response = getRemote().post(getResourceUri() + "/logs/query", params, query); return getFactory().logEntries(getCluster(), response); } @Override public LogEntry readLogEntry(String logEntryId) { LogEntry logEntry = null; try { Response response = getRemote().get(getResourceUri() + "/logs/" + logEntryId); logEntry = getFactory().logEntry(getCluster(), response); } catch (Exception ex) { // swallow for the time being // TODO: the remote layer needs to hand back more interesting more interesting // TODO: information so that we can detect a proper 404 } return logEntry; } @Override public void assignDataStore(PlatformDataStore datastore) { assignDataStore(datastore, null); } @Override public void assignDataStore(PlatformDataStore datastore, String key) { Map<String, String> params = new HashMap<String, String>(); params.put("id", datastore.getId()); params.put("key", datastore.getId()); params.put("type", datastore.getTypeId()); if (key != null) { params.put("key", key); } getRemote().post(getResourceUri() + "/datastores/assign", params); } @Override public void unassignDataStore(String key) { Map<String, String> params = new HashMap<String, String>(); params.put("key", key); getRemote().post(getResourceUri() + "/datastores/unassign", params); } @Override public ResultMap<PlatformDataStore> listDataStores() { return listDataStores(null); } @Override public ResultMap<PlatformDataStore> listDataStores(Pagination pagination) { Map<String, String> params = DriverUtil.params(pagination); Response response = getRemote().get(getResourceUri() + "/datastores", params); // THIS FAILS BECAUSE _DOC on the response for each item is KEY! // so a follow up .delete() on any of the items fails return getFactory().platformDataStores(this.getPlatform(), response); } @Override public ResultMap<PlatformDataStore> queryDataStores(ObjectNode query) { return queryDataStores(query, null); } @Override public ResultMap<PlatformDataStore> queryDataStores(ObjectNode query, Pagination pagination) { Map<String, String> params = DriverUtil.params(pagination); Response response = getRemote().post(getResourceUri() + "/datastores/query", params, query); return getFactory().platformDataStores(this.getPlatform(), response); } @Override public boolean existsDataStore(String key) { boolean exists = false; Response response = getRemote().post(getResourceUri() + "/datastores/exists?key=" + key); if (response.getObjectNode().has("exists")) { exists = response.getObjectNode().get("exists").booleanValue(); } return exists; } @Override public DataStore readDataStore(String key) { DataStore datastore = null; try { Response response = getRemote().get(getResourceUri() + "/datastores/" + key); datastore = getFactory().platformDataStore(getPlatform(), response.getObjectNode()); // some correction } catch (Exception ex) { // swallow for the time being // TODO: the remote layer needs to hand back more interesting more interesting // TODO: information so that we can detect a proper 404 } return datastore; } }