org.labkey.remoteapi.security.GetContainersResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of labkey-client-api Show documentation
Show all versions of labkey-client-api Show documentation
The client-side library for Java developers is a separate JAR from the LabKey Server code base. It can be used by any Java program, including another Java web application.
/*
* Copyright (c) 2008-2009 LabKey Corporation
*
* 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 org.labkey.remoteapi.security;
import org.json.JSONObject;
import org.labkey.remoteapi.CommandResponse;
import java.util.List;
import java.util.Map;
public class GetContainersResponse extends CommandResponse
{
public GetContainersResponse(String text, int statusCode, String contentType, JSONObject json)
{
super(text, statusCode, contentType, json);
}
public String getContainerId()
{
return getProperty("id");
}
public Integer getUserPermissions()
{
return getUserPermissions(null);
}
public Integer getUserPermissions(String containerPath)
{
Map containerInfo = findContainer(containerPath, getParsedData());
return (null == containerInfo) ? null : ((Number)containerInfo.get("userPermissions")).intValue();
}
public Boolean hasPermission(int perm)
{
Integer userPerms = getUserPermissions();
return (null == userPerms) ? null : userPerms == (userPerms | perm);
}
public Boolean hasPermission(int perm, String containerPath)
{
Integer userPerms = getUserPermissions(containerPath);
return (null == userPerms) ? null : userPerms == (userPerms | perm);
}
protected Map findContainer(String containerPath, Map root)
{
if(null == containerPath)
return root; //assumed by code above
containerPath = containerPath.replace('\\', '/');
if(containerPath.startsWith("/"))
containerPath = containerPath.substring(1);
String[] pathParts = containerPath.split("/");
return findContainer(pathParts, 0, root);
}
@SuppressWarnings("unchecked")
protected Map findContainer(String[] containerPath, int index, Map root)
{
//if current root name doesn't match this path part, return null
if(!containerPath[index].equalsIgnoreCase((String)root.get("name")))
return null;
//if this is the last path part, return root
if(containerPath.length - 1 == index)
return root;
List