com.tangosol.internal.management.MBeanResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of coherence Show documentation
Show all versions of coherence Show documentation
Oracle Coherence Community Edition
/*
* Copyright (c) 2000, 2022, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.tangosol.internal.management;
import com.tangosol.internal.http.HttpRequest;
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;
/**
* 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 request the {@link HttpRequest}
*/
public MBeanResponse(HttpRequest request)
{
this(request, (Filter) null);
}
/**
* Construct a MBeanResponse instance.
*
* @param request the {@link HttpRequest}
* @param filterLinks the links filter
*/
public MBeanResponse(HttpRequest request, Filter filterLinks)
{
m_fIncludeResourceLinks = !Boolean.parseBoolean(request.getHeaderString(HEADER_SKIP_LINKS));
if (filterLinks == null)
{
filterLinks = Filters.always();
}
m_filterLinks = filterLinks;
}
// ----- MBeanResponse methods ------------------------------------------------------
/**
* Returns {@code true} if there are any FAILURE messages in the response.
*
* @return {@code true} if there are any FAILURE messages in the response
*/
public boolean hasFailures()
{
return m_listMessages.size() != 0 &&
m_listMessages.stream().anyMatch(m -> m.f_sSeverity == Message.Severity.FAILURE);
}
/**
* 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