Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public DbException(String message, Exception cause)
{
super(message, cause);
}

public DbException(String message)
{
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@

package org.opendcs.odcsapi.res;

import java.sql.SQLException;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import java.util.regex.Pattern;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
Expand All @@ -31,19 +35,25 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import decodes.db.DataSource;
import decodes.db.DataSourceList;
import decodes.db.DatabaseException;
import decodes.db.DatabaseIO;
import decodes.sql.DbKey;
import org.opendcs.odcsapi.beans.ApiDataSource;
import org.opendcs.odcsapi.dao.ApiDataSourceDAO;
import org.opendcs.odcsapi.beans.ApiDataSourceGroupMember;
import org.opendcs.odcsapi.beans.ApiDataSourceRef;
import org.opendcs.odcsapi.dao.DbException;
import org.opendcs.odcsapi.errorhandling.ErrorCodes;
import org.opendcs.odcsapi.errorhandling.WebAppException;
import org.opendcs.odcsapi.hydrojson.DbInterface;
import org.opendcs.odcsapi.sec.AuthorizationCheck;
import org.opendcs.odcsapi.util.ApiConstants;
import org.opendcs.odcsapi.util.ApiHttpUtil;

@Path("/")
public class DataSourceResources
public class DataSourceResources extends OpenDcsResource
{
private DatabaseIO dbIo;
private static final Pattern COMMA = Pattern.compile(",");
private static final Pattern EQUAL = Pattern.compile("=");

@Context HttpHeaders httpHeaders;

@GET
Expand All @@ -52,80 +62,285 @@ public class DataSourceResources
@RolesAllowed({AuthorizationCheck.ODCS_API_GUEST})
public Response getDataSourceRefs() throws DbException
{
Logger.getLogger(ApiConstants.loggerName).fine("getDataSourceRefs");
try (DbInterface dbi = new DbInterface();
ApiDataSourceDAO dao = new ApiDataSourceDAO(dbi))
try
{
dbIo = getLegacyDatabase();
DataSourceList dsl = new DataSourceList();
dbIo.readDataSourceList(dsl);
return Response.status(HttpServletResponse.SC_OK).entity(map(dsl)).build();
}
catch (DatabaseException ex)
{
throw new DbException("Error reading data source list: " + ex);
}
finally
{
dbIo.close();
}
}

static ArrayList<ApiDataSourceRef> map(DataSourceList dsl)
{
ArrayList<ApiDataSourceRef> ret = new ArrayList<>();
for(DataSource ds : dsl.getList())
{
return ApiHttpUtil.createResponse(dao.readDataSourceRefs());
ApiDataSourceRef adr = new ApiDataSourceRef();
if (ds.getId() != null)
{
adr.setDataSourceId(ds.getId().getValue());
}
else
{
adr.setDataSourceId(DbKey.NullKey.getValue());
}
adr.setName(ds.getName());
adr.setType(ds.dataSourceType);
adr.setUsedBy(ds.numUsedBy);
if (ds.getArguments() != null)
{
adr.setArguments(propsToString(ds.getArguments()));
}
else
{
adr.setArguments(ds.getDataSourceArg());
}
ret.add(adr);
}
return ret;
}

static String propsToString(Properties props)
{
if (props == null || props.isEmpty())
return "";

StringBuilder retVal = new StringBuilder();
for (Object key : props.keySet())
{
retVal.append(key).append("=").append(props.getProperty((String) key)).append(",");
}
return retVal.substring(0, retVal.length() - 1);
}

@GET
@Path("datasource")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({AuthorizationCheck.ODCS_API_GUEST})
public Response geDataSource(@QueryParam("datasourceid") Long dataSourceId)
throws WebAppException, DbException, SQLException
public Response getDataSource(@QueryParam("datasourceid") Long dataSourceId)
throws WebAppException, DbException
{
String notFound = "No such DECODES data source with id=";
if (dataSourceId == null)
throw new WebAppException(ErrorCodes.MISSING_ID,
"Missing required datasourceid parameter.");

Logger.getLogger(ApiConstants.loggerName).fine("getDataSource id=" + dataSourceId);
try (DbInterface dbi = new DbInterface();
ApiDataSourceDAO dao = new ApiDataSourceDAO(dbi))
{
ApiDataSource ret = dao.readDataSource(dataSourceId);
if (ret == null)
throw new WebAppException(ErrorCodes.NO_SUCH_OBJECT,
"No such DECODES data source with id=" + dataSourceId + ".");
return ApiHttpUtil.createResponse(ret);
{
throw new WebAppException(HttpServletResponse.SC_BAD_REQUEST,
"Missing required datasourceid parameter.");
}

try
{
dbIo = getLegacyDatabase();
DataSource ds = new DataSource(DbKey.createDbKey(dataSourceId));
dbIo.readDataSource(ds);

if (ds.getName() == null)
{
throw new WebAppException(HttpServletResponse.SC_NOT_FOUND,
notFound + dataSourceId + ".");
}
ApiDataSource ret = map(ds);
return Response.status(HttpServletResponse.SC_OK).entity(ret).build();
}
catch (DatabaseException ex)
{
if (ex.getMessage().contains("No DataSource found with id"))
{
return Response.status(HttpServletResponse.SC_NOT_FOUND)
.entity(notFound + dataSourceId + ".").build();
}
throw new DbException("Error reading data source: " + ex);
}
finally
{
dbIo.close();
}
}

static ApiDataSource map(DataSource ds)
{
if (ds == null)
return null;
ApiDataSource ads = new ApiDataSource();
if (ds.getId() != null)
{
ads.setDataSourceId(ds.getId().getValue());
}
else
{
ads.setDataSourceId(DbKey.NullKey.getValue());
}
ads.setName(ds.getName());
ads.setType(ds.dataSourceType);
if (ds.getArguments() != null)
{
ads.setProps(ds.getArguments());
}
else
{
ads.setProps(parseProps(ds.getDataSourceArg()));
}
ads.setGroupMembers(map(ds.groupMembers));
ads.setUsedBy(ds.numUsedBy);
return ads;
}

static List<ApiDataSourceGroupMember> map(Vector<DataSource> groupMembers)
{
if (groupMembers == null)
{
return new ArrayList<>();
}
List<ApiDataSourceGroupMember> ret = new ArrayList<>();
for(DataSource ds : groupMembers)
{
ApiDataSourceGroupMember ads = new ApiDataSourceGroupMember();
if (ds.getId() != null)
{
ads.setDataSourceId(ds.getId().getValue());
}
else
{
ads.setDataSourceId(DbKey.NullKey.getValue());
}
ads.setDataSourceName(ds.getName());
ret.add(ads);
}
return ret;
}

@POST
@Path("datasource")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({AuthorizationCheck.ODCS_API_ADMIN, AuthorizationCheck.ODCS_API_USER})
public Response postDatasource(ApiDataSource datasource) throws WebAppException, DbException, SQLException
public Response postDatasource(ApiDataSource datasource) throws DbException, WebAppException
{
if (datasource == null)
{
throw new WebAppException(HttpServletResponse.SC_BAD_REQUEST,
"Missing required data source object.");
}
try
{
dbIo = getLegacyDatabase();
DataSource source = map(datasource);
dbIo.writeDataSource(source);
return Response.status(HttpServletResponse.SC_OK)
.entity(map(source))
.build();
}
catch (DatabaseException ex)
{
throw new DbException("Error writing data source: " + ex);
}
finally
{
dbIo.close();
}
}

static DataSource map(ApiDataSource ads) throws DatabaseException
{
Logger.getLogger(ApiConstants.loggerName).fine(
"post datasource received datasource " + datasource.getName()
+ " with ID=" + datasource.getDataSourceId());

try (DbInterface dbi = new DbInterface();
ApiDataSourceDAO dsDao = new ApiDataSourceDAO(dbi))
DataSource ds = new DataSource();
if (ads.getDataSourceId() != null)
{
ds.setId(DbKey.createDbKey(ads.getDataSourceId()));
}
else
{
dsDao.writedDataSource(datasource);
return ApiHttpUtil.createResponse(datasource);
ds.setId(DbKey.NullKey);
}
ds.setName(ads.getName());
ds.dataSourceType = ads.getType();
ds.arguments = ads.getProps();
ds.setDataSourceArg(propsToString(ads.getProps()));
ds.numUsedBy = ads.getUsedBy();
ds.groupMembers = map(ads.getGroupMembers());
return ds;
}

static Vector<DataSource> map(List<ApiDataSourceGroupMember> groupMembers)
{
Vector<DataSource> ret = new Vector<>();
if (groupMembers == null)
{
return ret;
}
for(ApiDataSourceGroupMember ads : groupMembers)
{
DataSource ds = new DataSource(DbKey.createDbKey(ads.getDataSourceId()));
ds.setName(ads.getDataSourceName());
ret.add(ds);
}
return ret;
}

@DELETE
@Path("datasource")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({AuthorizationCheck.ODCS_API_ADMIN, AuthorizationCheck.ODCS_API_USER})
public Response deleteDatasource(@QueryParam("datasourceid") Long datasourceId) throws DbException
public Response deleteDatasource(@QueryParam("datasourceid") Long datasourceId) throws DbException, WebAppException
{
Logger.getLogger(ApiConstants.loggerName).fine(
"DELETE datasource received datasourceid=" + datasourceId);

// Use username and password to attempt to connect to the database
try (DbInterface dbi = new DbInterface();
ApiDataSourceDAO dsDao = new ApiDataSourceDAO(dbi))
{
String errmsg = dsDao.datasourceUsedByRs(datasourceId);
if (errmsg != null)
return ApiHttpUtil.createResponse(" Cannot delete datasource with ID " + datasourceId
+ " because it is used by the following routing specs: "
+ errmsg, ErrorCodes.NOT_ALLOWED);

dsDao.deleteDatasource(datasourceId);
return ApiHttpUtil.createResponse("Datasource with ID " + datasourceId + " deleted");
if (datasourceId == null)
{
throw new WebAppException(HttpServletResponse.SC_BAD_REQUEST, "Missing required datasourceid parameter.");
}
}
try
{
dbIo = getLegacyDatabase();
DataSource ds = new DataSource(DbKey.createDbKey(datasourceId));
dbIo.readDataSource(ds);
if (ds.getName() == null)
{
return Response.status(HttpServletResponse.SC_NOT_FOUND)
.entity("No such data source with ID " + datasourceId).build();
}

if (ds.numUsedBy > 0)
{
return Response.status(HttpServletResponse.SC_METHOD_NOT_ALLOWED)
.entity(" Cannot delete datasource with ID " + datasourceId
+ " because it is used by the following number of routing specs: "
+ ds.numUsedBy).build();
}

dbIo.deleteDataSource(ds);
return Response.status(HttpServletResponse.SC_OK)
.entity("Datasource with ID " + datasourceId + " deleted").build();
}
catch (DatabaseException ex)
{
throw new DbException("Error deleting data source: " + ex);
}
finally
{
dbIo.close();
}
}

static Properties parseProps(String properties)
{
if (properties == null || properties.isEmpty())
{
return new Properties();
}
Properties props = new Properties();
String[] pairs = properties.split(COMMA.pattern());
for (String pair : pairs)
{
String[] keyValue = pair.split(EQUAL.pattern());
props.setProperty(keyValue[0], keyValue[1]);
}
return props;
}
}
Loading