
io.apiman.manager.api.es.EsMarshalling Maven / Gradle / Ivy
/*
* Copyright 2015 JBoss 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.
*/
package io.apiman.manager.api.es;
import io.apiman.manager.api.beans.apis.ApiBean;
import io.apiman.manager.api.beans.apis.ApiDefinitionType;
import io.apiman.manager.api.beans.apis.ApiGatewayBean;
import io.apiman.manager.api.beans.apis.ApiPlanBean;
import io.apiman.manager.api.beans.apis.ApiStatus;
import io.apiman.manager.api.beans.apis.ApiVersionBean;
import io.apiman.manager.api.beans.apis.EndpointContentType;
import io.apiman.manager.api.beans.apis.EndpointType;
import io.apiman.manager.api.beans.audit.AuditEntityType;
import io.apiman.manager.api.beans.audit.AuditEntryBean;
import io.apiman.manager.api.beans.audit.AuditEntryType;
import io.apiman.manager.api.beans.clients.ClientBean;
import io.apiman.manager.api.beans.clients.ClientStatus;
import io.apiman.manager.api.beans.clients.ClientVersionBean;
import io.apiman.manager.api.beans.contracts.ContractBean;
import io.apiman.manager.api.beans.download.DownloadBean;
import io.apiman.manager.api.beans.download.DownloadType;
import io.apiman.manager.api.beans.gateways.GatewayBean;
import io.apiman.manager.api.beans.gateways.GatewayType;
import io.apiman.manager.api.beans.idm.PermissionType;
import io.apiman.manager.api.beans.idm.RoleBean;
import io.apiman.manager.api.beans.idm.RoleMembershipBean;
import io.apiman.manager.api.beans.idm.UserBean;
import io.apiman.manager.api.beans.orgs.OrganizationBean;
import io.apiman.manager.api.beans.plans.PlanBean;
import io.apiman.manager.api.beans.plans.PlanStatus;
import io.apiman.manager.api.beans.plans.PlanVersionBean;
import io.apiman.manager.api.beans.plugins.PluginBean;
import io.apiman.manager.api.beans.policies.PolicyBean;
import io.apiman.manager.api.beans.policies.PolicyDefinitionBean;
import io.apiman.manager.api.beans.policies.PolicyDefinitionTemplateBean;
import io.apiman.manager.api.beans.policies.PolicyType;
import io.apiman.manager.api.beans.summary.ApiEntryBean;
import io.apiman.manager.api.beans.summary.ApiSummaryBean;
import io.apiman.manager.api.beans.summary.ApiVersionSummaryBean;
import io.apiman.manager.api.beans.summary.ClientSummaryBean;
import io.apiman.manager.api.beans.summary.ClientVersionSummaryBean;
import io.apiman.manager.api.beans.summary.ContractSummaryBean;
import io.apiman.manager.api.beans.summary.GatewaySummaryBean;
import io.apiman.manager.api.beans.summary.OrganizationSummaryBean;
import io.apiman.manager.api.beans.summary.PlanSummaryBean;
import io.apiman.manager.api.beans.summary.PlanVersionSummaryBean;
import io.apiman.manager.api.beans.summary.PluginSummaryBean;
import io.apiman.manager.api.beans.summary.PolicyDefinitionSummaryBean;
import io.apiman.manager.api.beans.summary.PolicyFormType;
import io.apiman.manager.api.core.exceptions.StorageException;
import io.apiman.manager.api.es.beans.ApiDefinitionBean;
import io.apiman.manager.api.es.beans.PoliciesBean;
import io.apiman.manager.api.es.util.XContentBuilder;
import io.apiman.manager.api.es.util.XContentFactory;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* Marshalls objects into Maps to be used in ES requests. Also unmarshalls from
* maps back into objects.
*
* @author [email protected]
*/
@SuppressWarnings("nls")
public class EsMarshalling {
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(PoliciesBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("organizationId", bean.getOrganizationId())
.field("entityId", bean.getEntityId())
.field("entityVersion", bean.getEntityVersion())
.field("type", bean.getType());
List policies = bean.getPolicies();
if (policies != null && !policies.isEmpty()) {
builder.startArray("policies");
for (PolicyBean policy : policies) {
builder.startObject()
.field("id", policy.getId())
.field("name", policy.getName())
.field("configuration", policy.getConfiguration())
.field("createdBy", policy.getCreatedBy())
.field("createdOn", policy.getCreatedOn().getTime())
.field("modifiedBy", policy.getModifiedBy())
.field("modifiedOn", policy.getModifiedOn().getTime())
.field("definitionId", policy.getDefinition().getId())
.field("orderIndex", policy.getOrderIndex())
.endObject();
}
builder.endArray();
}
builder.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(GatewayBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("id", bean.getId())
.field("name", bean.getName())
.field("description", bean.getDescription())
.field("type", bean.getType())
.field("configuration", bean.getConfiguration())
.field("createdBy", bean.getCreatedBy())
.field("createdOn", bean.getCreatedOn().getTime())
.field("modifiedBy", bean.getModifiedBy())
.field("modifiedOn", bean.getModifiedOn().getTime())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(ApiDefinitionBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("data", bean.getData())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(ContractBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("id", bean.getId())
.field("clientOrganizationId", bean.getClient().getClient().getOrganization().getId())
.field("clientOrganizationName", bean.getClient().getClient().getOrganization().getName())
.field("clientId", bean.getClient().getClient().getId())
.field("clientName", bean.getClient().getClient().getName())
.field("clientVersion", bean.getClient().getVersion())
.field("apiOrganizationId", bean.getApi().getApi().getOrganization().getId())
.field("apiOrganizationName", bean.getApi().getApi().getOrganization().getName())
.field("apiId", bean.getApi().getApi().getId())
.field("apiName", bean.getApi().getApi().getName())
.field("apiVersion", bean.getApi().getVersion())
.field("apiDescription", bean.getApi().getApi().getDescription())
.field("planName", bean.getPlan().getPlan().getName())
.field("planId", bean.getPlan().getPlan().getId())
.field("planVersion", bean.getPlan().getVersion())
.field("createdOn", bean.getCreatedOn().getTime())
.field("createdBy", bean.getCreatedBy())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(PlanBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("organizationId", bean.getOrganization().getId())
.field("organizationName", bean.getOrganization().getName())
.field("id", bean.getId())
.field("name", bean.getName())
.field("description", bean.getDescription())
.field("createdBy", bean.getCreatedBy())
.field("createdOn", bean.getCreatedOn().getTime())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(PlanVersionBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
PlanBean plan = bean.getPlan();
OrganizationBean org = plan.getOrganization();
preMarshall(bean);
builder
.startObject()
.field("organizationId", org.getId())
.field("organizationName", org.getName())
.field("planId", plan.getId())
.field("planName", plan.getName())
.field("planDescription", plan.getDescription())
.field("version", bean.getVersion())
.field("status", bean.getStatus())
.field("createdBy", bean.getCreatedBy())
.field("createdOn", bean.getCreatedOn().getTime())
.field("modifiedBy", bean.getModifiedBy())
.field("modifiedOn", bean.getModifiedOn().getTime())
.field("lockedOn", bean.getLockedOn() != null ? bean.getLockedOn().getTime() : null)
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(ApiBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("organizationId", bean.getOrganization().getId())
.field("organizationName", bean.getOrganization().getName())
.field("id", bean.getId())
.field("name", bean.getName())
.field("description", bean.getDescription())
.field("createdBy", bean.getCreatedBy())
.field("createdOn", bean.getCreatedOn().getTime())
.field("numPublished", bean.getNumPublished())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(ApiVersionBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
ApiBean api = bean.getApi();
OrganizationBean org = api.getOrganization();
preMarshall(bean);
builder
.startObject()
.field("organizationId", org.getId())
.field("organizationName", org.getName())
.field("apiId", api.getId())
.field("apiName", api.getName())
.field("apiDescription", api.getDescription())
.field("version", bean.getVersion())
.field("status", bean.getStatus())
.field("createdBy", bean.getCreatedBy())
.field("createdOn", bean.getCreatedOn().getTime())
.field("modifiedBy", bean.getModifiedBy())
.field("modifiedOn", bean.getModifiedOn().getTime())
.field("publishedOn", bean.getPublishedOn() != null ? bean.getPublishedOn().getTime() : null)
.field("retiredOn", bean.getRetiredOn() != null ? bean.getRetiredOn().getTime() : null)
.field("publicAPI", bean.isPublicAPI())
.field("endpoint", bean.getEndpoint())
.field("endpointType", bean.getEndpointType())
.field("endpointContentType", bean.getEndpointContentType())
.field("parsePayload", bean.isParsePayload())
.field("definitionType", bean.getDefinitionType());
Set gateways = bean.getGateways();
if (gateways != null) {
builder.startArray("gateways");
for (ApiGatewayBean gateway : gateways) {
builder.startObject()
.field("gatewayId", gateway.getGatewayId())
.endObject();
}
builder.endArray();
}
Set plans = bean.getPlans();
if (plans != null) {
builder.startArray("plans");
for (ApiPlanBean plan : plans) {
builder.startObject()
.field("planId", plan.getPlanId())
.field("version", plan.getVersion())
.endObject();
}
builder.endArray();
}
Map endpointProperties = bean.getEndpointProperties();
if (endpointProperties != null) {
builder.startObject("endpointProperties");
for (Entry property : endpointProperties.entrySet()) {
builder.field(property.getKey(), property.getValue());
}
builder.endObject();
}
builder.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(ClientBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("organizationId", bean.getOrganization().getId())
.field("organizationName", bean.getOrganization().getName())
.field("id", bean.getId())
.field("name", bean.getName())
.field("description", bean.getDescription())
.field("createdBy", bean.getCreatedBy())
.field("createdOn", bean.getCreatedOn().getTime())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(ClientVersionBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
ClientBean client = bean.getClient();
OrganizationBean org = client.getOrganization();
preMarshall(bean);
builder
.startObject()
.field("organizationId", org.getId())
.field("organizationName", org.getName())
.field("clientId", client.getId())
.field("clientName", client.getName())
.field("clientDescription", client.getDescription())
.field("version", bean.getVersion())
.field("apikey", bean.getApikey())
.field("status", bean.getStatus())
.field("createdBy", bean.getCreatedBy())
.field("createdOn", bean.getCreatedOn().getTime())
.field("modifiedBy", bean.getModifiedBy())
.field("modifiedOn", bean.getModifiedOn().getTime())
.field("publishedOn", bean.getPublishedOn() != null ? bean.getPublishedOn().getTime() : null)
.field("retiredOn", bean.getRetiredOn() != null ? bean.getRetiredOn().getTime() : null)
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(AuditEntryBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("id", bean.getId())
.field("organizationId", bean.getOrganizationId())
.field("entityId", bean.getEntityId())
.field("entityType", bean.getEntityType())
.field("entityVersion", bean.getEntityVersion())
.field("data", bean.getData())
.field("who", bean.getWho())
.field("what", bean.getWhat())
.field("createdOn", bean.getCreatedOn().getTime())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(OrganizationBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("id", bean.getId())
.field("name", bean.getName())
.field("description", bean.getDescription())
.field("createdBy", bean.getCreatedBy())
.field("createdOn", bean.getCreatedOn().getTime())
.field("modifiedBy", bean.getModifiedBy())
.field("modifiedOn", bean.getModifiedOn().getTime())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(RoleMembershipBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("id", bean.getId())
.field("organizationId", bean.getOrganizationId())
.field("roleId", bean.getRoleId())
.field("userId", bean.getUserId())
.field("createdOn", bean.getCreatedOn().getTime())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(UserBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("username", bean.getUsername())
.field("email", bean.getEmail())
.field("fullName", bean.getFullName())
.field("joinedOn", bean.getJoinedOn() == null ? null : bean.getJoinedOn().getTime())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(RoleBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("id", bean.getId())
.field("name", bean.getName())
.field("description", bean.getDescription())
.field("createdBy", bean.getCreatedBy())
.field("createdOn", bean.getCreatedOn().getTime())
.field("autoGrant", bean.getAutoGrant());
Set permissions = bean.getPermissions();
if (permissions != null && !permissions.isEmpty()) {
builder.array("permissions", permissions.toArray(new PermissionType[permissions.size()]));
}
builder.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(PolicyDefinitionBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("id", bean.getId())
.field("name", bean.getName())
.field("description", bean.getDescription())
.field("form", bean.getForm())
.field("formType", bean.getFormType())
.field("icon", bean.getIcon())
.field("pluginId", bean.getPluginId())
.field("policyImpl", bean.getPolicyImpl())
.field("deleted", bean.isDeleted());
Set templates = bean.getTemplates();
if (templates != null) {
builder.field("templates").startArray();
for (PolicyDefinitionTemplateBean template : templates) {
builder.startObject();
builder.field("language", template.getLanguage());
builder.field("template", template.getTemplate());
builder.endObject();
}
builder.endArray();
}
builder.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(PluginBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("id", bean.getId())
.field("name", bean.getName())
.field("description", bean.getDescription())
.field("createdBy", bean.getCreatedBy())
.field("createdOn", bean.getCreatedOn().getTime())
.field("groupId", bean.getGroupId())
.field("artifactId", bean.getArtifactId())
.field("version", bean.getVersion())
.field("classifier", bean.getClassifier())
.field("type", bean.getType())
.field("deleted", bean.isDeleted())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Marshals the given bean into the given map.
* @param bean the bean
* @return the content builder
* @throws StorageException when a storage problem occurs while storing a bean
*/
public static XContentBuilder marshall(DownloadBean bean) throws StorageException {
try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
preMarshall(bean);
builder
.startObject()
.field("id", bean.getId())
.field("type", bean.getType().name())
.field("path", bean.getPath())
.field("expires", bean.getExpires().getTime())
.endObject();
postMarshall(bean);
return builder;
} catch (IOException e) {
throw new StorageException(e);
}
}
/**
* Unmarshals the given map source into a bean.
* @param source the source
* @return the policy beans
*/
@SuppressWarnings("unchecked")
public static PoliciesBean unmarshallPolicies(Map source) {
if (source == null) {
return null;
}
PoliciesBean bean = new PoliciesBean();
bean.setOrganizationId(asString(source.get("organizationId")));
bean.setEntityId(asString(source.get("entityId")));
bean.setEntityVersion(asString(source.get("entityVersion")));
bean.setType(asEnum(source.get("type"), PolicyType.class));
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy