com.tangosol.coherence.management.internal.MBeanResponse Maven / Gradle / Ivy
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
package com.tangosol.coherence.management.internal;
import com.tangosol.util.Filter;
import com.tangosol.util.Filters;
import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.container.ContainerRequestContext;
/**
* Response wrapper object to be used for Coherence management over REST implementation.
*
* @author sr 2017.08.21
* @since 12.2.1.4.0
*/
public class MBeanResponse
{
// ----- constructors ---------------------------------------------------
/**
* Default Constructor.
*/
public MBeanResponse()
{
}
/**
* Construct an MBeanResponse instance.
*
* @param ctxRequest the request context
*/
public MBeanResponse(ContainerRequestContext ctxRequest)
{
this(ctxRequest, (Filter) null);
}
/**
* Construct a MBeanResponse instance.
*
* @param requestContext the request context
* @param filterLinks the links filter
*/
public MBeanResponse(ContainerRequestContext requestContext, Filter filterLinks)
{
m_fIncludeResourceLinks = !Boolean.valueOf(requestContext.getHeaderString(HEADER_SKIP_LINKS));
if (filterLinks == null)
{
filterLinks = Filters.always();
}
m_filterLinks = filterLinks;
}
// ----- MBeanResponse methods ------------------------------------------------------
/**
* Returns true if there are any FAILURE messages in the response.
*
* @return true if there are any FAILURE messages in the response
*/
public boolean hasFailures()
{
return m_listMessages.size() != 0 &&
m_listMessages.stream().filter(m -> m.f_sSeverity == Message.Severity.FAILURE).count() > 0;
}
/**
* Add a failure message to the response.
*
* @param sMessage the message to be added
*/
public void addFailure(String sMessage)
{
addMessage(Message.Severity.FAILURE, sMessage);
}
/**
* Add a failure message for the corresponding field to the response. Used
* in case of failures in put request to an Mbean, where an attribute updated failed.
*
* @param sField the field which failed
* @param sMessage the message to be added
*/
public void addFailure(String sField, String sMessage)
{
addMessage(Message.Severity.FAILURE, sField, sMessage);
}
/**
* Add a message to the response.
*
* @param severity the severity of the response
* @param sField the field which failed
* @param sMessage the message to be added
*/
public void addMessage(Message.Severity severity, String sField, String sMessage)
{
add(new Message(severity, sField, sMessage));
}
/**
* Add a message to the response.
* @param severity the severity of the response
* @param sMessage the message to be added
*/
public void addMessage(Message.Severity severity, String sMessage)
{
add(new Message(severity, sMessage));
}
/**
* Add a message to the response.
*
* @param message the message to be added
*/
public void add(Message message)
{
m_listMessages.add(message);
}
/**
* Add the URI as the self link of the resource.
*
* @param uri the URI of the resource
*/
public void addSelfResourceLinks(URI uri)
{
addResourceLink(LINK_REL_SELF, uri);
addResourceLink(LINK_REL_CANONICAL, uri);
}
/**
* Add the URI as the parent link of the resource.
*
* @param uri the URI of the parent resource
*/
public void addParentResourceLink(URI uri)
{
addResourceLink(LINK_REL_PARENT, uri);
}
/**
* Add a resource link to the response links.
*
* @param sRel the name of link
* @param uri the URI of the link
* @return
*/
public void addResourceLink(String sRel, URI uri)
{
ResourceLink linkRes = new ResourceLink(sRel, uri);
if (m_filterLinks.evaluate(linkRes.getRelationship()))
{
m_listLinks.add(linkRes);
}
}
/**
* Convert the response body to a Json object.
* @return the equivalent JSON object
*/
public Map toJson()
{
Map mapResponse = new LinkedHashMap<>();
populateJson(mapResponse);
return mapResponse;
}
/**
* Populate the body to the provided JSON object. The action included adding
* links, messages to the response.
*
* @param mapResponse the JSON Object to be populated
*/
protected void populateJson(Map mapResponse)
{
List listMessages = m_listMessages;
if (!listMessages.isEmpty())
{
List