Search in sources :

Example 71 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project iaf by ibissource.

the class ExecuteJdbcQuery method execute.

@POST
@RolesAllowed({ "IbisTester" })
@Path("/jdbc/query")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response execute(LinkedHashMap<String, Object> json) throws ApiException {
    String datasource = null, resultType = null, query = null, queryType = null, result = "", returnType = MediaType.APPLICATION_XML;
    boolean avoidLocking = false, trimSpaces = false;
    for (Entry<String, Object> entry : json.entrySet()) {
        String key = entry.getKey();
        if (key.equalsIgnoreCase("datasource")) {
            datasource = entry.getValue().toString();
        }
        if (key.equalsIgnoreCase("resultType")) {
            resultType = entry.getValue().toString().toLowerCase();
            if (resultType.equalsIgnoreCase("csv")) {
                returnType = MediaType.TEXT_PLAIN;
            }
            if (resultType.equalsIgnoreCase("json")) {
                returnType = MediaType.APPLICATION_JSON;
            }
        }
        if (key.equalsIgnoreCase("avoidLocking")) {
            avoidLocking = Boolean.parseBoolean(entry.getValue().toString());
        }
        if (key.equalsIgnoreCase("trimSpaces")) {
            trimSpaces = Boolean.parseBoolean(entry.getValue().toString());
        }
        if (key.equalsIgnoreCase("query")) {
            query = entry.getValue().toString();
        }
        if (key.equalsIgnoreCase("queryType")) {
            queryType = entry.getValue().toString();
        }
    }
    if ("AUTO".equals(queryType)) {
        // defaults to other
        queryType = "other";
        // if it matches, set it to select
        String[] commands = new String[] { "select", "show" };
        for (String command : commands) {
            if (query.toLowerCase().startsWith(command)) {
                queryType = "select";
                break;
            }
        }
    }
    if (datasource == null || resultType == null || query == null) {
        throw new ApiException("Missing data, datasource, resultType and query are expected.", 400);
    }
    secLog.info(String.format("executing query [%s] on datasource [%s] queryType [%s] avoidLocking [%s]", query, datasource, queryType, avoidLocking));
    // We have all info we need, lets execute the query!
    DirectQuerySender qs;
    try {
        qs = (DirectQuerySender) getIbisContext().createBeanAutowireByName(DirectQuerySender.class);
    } catch (Exception e) {
        throw new ApiException("An error occured on creating or closing the connection", e);
    }
    try {
        qs.setName("QuerySender");
        qs.setDatasourceName(datasource);
        qs.setQueryType(queryType);
        qs.setTrimSpaces(trimSpaces);
        qs.setAvoidLocking(avoidLocking);
        qs.setBlobSmartGet(true);
        qs.setPrettyPrint(true);
        qs.configure(true);
        qs.open();
        Message message = qs.sendMessage(new Message(query), null);
        if (resultType.equalsIgnoreCase("csv")) {
            AbstractQueryOutputTransformer filter = new QueryOutputToCSV();
            result = filter.parse(message);
        } else if (resultType.equalsIgnoreCase("json")) {
            AbstractQueryOutputTransformer filter = new QueryOutputToJson();
            result = filter.parse(message);
        } else {
            result = message.asString();
        }
    } catch (Throwable t) {
        throw new ApiException("Error executing query", t);
    } finally {
        qs.close();
    }
    return Response.status(Response.Status.CREATED).type(returnType).entity(result).build();
}
Also used : QueryOutputToJson(nl.nn.adapterframework.jdbc.transformer.QueryOutputToJson) Message(nl.nn.adapterframework.stream.Message) QueryOutputToCSV(nl.nn.adapterframework.jdbc.transformer.QueryOutputToCSV) DirectQuerySender(nl.nn.adapterframework.jdbc.DirectQuerySender) AbstractQueryOutputTransformer(nl.nn.adapterframework.jdbc.transformer.AbstractQueryOutputTransformer) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 72 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project iaf by ibissource.

the class Init method getAllResources.

@GET
@PermitAll
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
public Response getAllResources(@QueryParam("allowedRoles") boolean displayAllowedRoles) {
    List<Object> JSONresources = new ArrayList<Object>();
    Map<String, Object> HALresources = new HashMap<String, Object>();
    Map<String, Object> resources = new HashMap<String, Object>(1);
    StringBuffer requestPath = httpServletRequest.getRequestURL();
    if (requestPath.substring(requestPath.length() - 1).equals("/"))
        requestPath.setLength(requestPath.length() - 1);
    for (ClassResourceInfo cri : getJAXRSService().getClassResourceInfo()) {
        MethodDispatcher methods = cri.getMethodDispatcher();
        for (OperationResourceInfo operation : methods.getOperationResourceInfos()) {
            Method method = operation.getMethodToInvoke();
            String relation = null;
            if (method.getDeclaringClass() == getClass()) {
                continue;
            }
            if (method.getDeclaringClass().getName().endsWith("ShowMonitors") && !AppConstants.getInstance().getBoolean("monitoring.enabled", false)) {
                continue;
            }
            Map<String, Object> resource = new HashMap<String, Object>(4);
            if (method.isAnnotationPresent(GET.class))
                resource.put("type", "GET");
            else if (method.isAnnotationPresent(POST.class))
                resource.put("type", "POST");
            else if (method.isAnnotationPresent(PUT.class))
                resource.put("type", "PUT");
            else if (method.isAnnotationPresent(DELETE.class))
                resource.put("type", "DELETE");
            Path path = method.getAnnotation(Path.class);
            if (path != null) {
                String p = path.value();
                if (!p.startsWith("/"))
                    p = "/" + p;
                resource.put("href", requestPath + p);
            }
            RolesAllowed rolesAllowed = method.getAnnotation(RolesAllowed.class);
            if (rolesAllowed != null && displayAllowedRoles) {
                resource.put("allowed", rolesAllowed.value());
            }
            if ((HATEOASImplementation.equalsIgnoreCase("hal"))) {
                if (method.isAnnotationPresent(Relation.class))
                    relation = method.getAnnotation(Relation.class).value();
                if (relation != null) {
                    if (HALresources.containsKey(relation)) {
                        Object prevRelation = HALresources.get(relation);
                        List<Object> tmpList = null;
                        if (prevRelation instanceof List)
                            tmpList = (List) prevRelation;
                        else {
                            tmpList = new ArrayList<Object>();
                            tmpList.add(prevRelation);
                        }
                        tmpList.add(resource);
                        HALresources.put(relation, tmpList);
                    } else
                        HALresources.put(relation, resource);
                }
            } else {
                if (method.isAnnotationPresent(Relation.class))
                    resource.put("rel", method.getAnnotation(Relation.class).value());
                JSONresources.add(resource);
            }
        }
    }
    if ((HATEOASImplementation.equalsIgnoreCase("hal")))
        resources.put(ResourceKey, HALresources);
    else
        resources.put(ResourceKey, JSONresources);
    return Response.status(Response.Status.CREATED).entity(resources).build();
}
Also used : Path(javax.ws.rs.Path) HashMap(java.util.HashMap) POST(javax.ws.rs.POST) ArrayList(java.util.ArrayList) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Method(java.lang.reflect.Method) DELETE(javax.ws.rs.DELETE) RolesAllowed(javax.annotation.security.RolesAllowed) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) ArrayList(java.util.ArrayList) List(java.util.List) MethodDispatcher(org.apache.cxf.jaxrs.model.MethodDispatcher) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) PermitAll(javax.annotation.security.PermitAll)

Example 73 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project iaf by ibissource.

the class ShowConfigurationStatus method getAdapterPipes.

@GET
@RolesAllowed({ "IbisObserver", "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/adapters/{name}/pipes")
@Produces(MediaType.APPLICATION_JSON)
public Response getAdapterPipes(@PathParam("name") String adapterName) throws ApiException {
    Adapter adapter = getAdapter(adapterName);
    ArrayList<Object> adapterInfo = mapAdapterPipes(adapter);
    if (adapterInfo == null)
        throw new ApiException("Adapter not configured!");
    return Response.status(Response.Status.OK).entity(adapterInfo).build();
}
Also used : Adapter(nl.nn.adapterframework.core.Adapter) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 74 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project iaf by ibissource.

the class SlotIdRecord method execute.

@POST
@RolesAllowed({ "IbisObserver", "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/jdbc/summary")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response execute(LinkedHashMap<String, Object> json) throws ApiException {
    // PUT defaults to no content
    Response.ResponseBuilder response = Response.noContent();
    String query = null;
    String datasource = null;
    for (Entry<String, Object> entry : json.entrySet()) {
        String key = entry.getKey();
        if (key.equalsIgnoreCase("datasource")) {
            datasource = entry.getValue().toString();
        }
        if (key.equalsIgnoreCase("query")) {
            query = entry.getValue().toString();
        }
    }
    if (datasource == null)
        return response.status(Response.Status.BAD_REQUEST).build();
    String result = "";
    try {
        IbisstoreSummaryQuerySender qs;
        qs = (IbisstoreSummaryQuerySender) getIbisContext().createBeanAutowireByName(IbisstoreSummaryQuerySender.class);
        qs.setSlotmap(getSlotmap());
        try {
            qs.setName("QuerySender");
            qs.setDatasourceName(datasource);
            qs.setQueryType("select");
            qs.setBlobSmartGet(true);
            qs.setAvoidLocking(true);
            qs.configure(true);
            qs.open();
            result = qs.sendMessage(new Message(query != null ? query : qs.getDbmsSupport().getIbisStoreSummaryQuery()), null).asString();
        } catch (Throwable t) {
            throw new ApiException("An error occured on executing jdbc query", t);
        } finally {
            qs.close();
        }
    } catch (Exception e) {
        throw new ApiException("An error occured on creating or closing the connection", e);
    }
    String resultObject = "{ \"result\":" + result + "}";
    return Response.status(Response.Status.CREATED).entity(resultObject).build();
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) Message(nl.nn.adapterframework.stream.Message) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) IOException(java.io.IOException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 75 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project iaf by ibissource.

the class ShowLogging method getLogDirectory.

@GET
@RolesAllowed({ "IbisObserver", "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/logging")
@Relation("logging")
@Produces(MediaType.APPLICATION_JSON)
public Response getLogDirectory(@QueryParam("directory") String directory, @QueryParam("sizeFormat") String sizeFormatParam, @QueryParam("wildcard") String wildcard) throws ApiException {
    Map<String, Object> returnMap = new HashMap<String, Object>();
    if (directory == null || directory.isEmpty())
        directory = AppConstants.getInstance().getResolvedProperty("logging.path").replace("\\\\", "\\");
    boolean sizeFormat = (sizeFormatParam == null || sizeFormatParam.isEmpty()) ? true : Boolean.parseBoolean(sizeFormatParam);
    if (wildcard == null || wildcard.isEmpty())
        wildcard = AppConstants.getInstance().getProperty("logging.wildcard");
    try {
        if (!FileUtils.readAllowed(FileViewerServlet.permissionRules, servletRequest, directory)) {
            throw new ApiException("Access to path (" + directory + ") not allowed!");
        }
        Dir2Map dir = new Dir2Map(directory, sizeFormat, wildcard, showDirectories, maxItems);
        returnMap.put("list", dir.getList());
        returnMap.put("count", dir.size());
        returnMap.put("directory", dir.getDirectory());
        returnMap.put("sizeFormat", sizeFormat);
        returnMap.put("wildcard", wildcard);
    } catch (IOException e) {
        throw new ApiException("Error while trying to retreive directory information", e);
    }
    return Response.status(Response.Status.OK).entity(returnMap).build();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Dir2Map(nl.nn.adapterframework.util.Dir2Map) IOException(java.io.IOException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

RolesAllowed (javax.annotation.security.RolesAllowed)191 Path (javax.ws.rs.Path)127 Produces (javax.ws.rs.Produces)110 Consumes (javax.ws.rs.Consumes)55 GET (javax.ws.rs.GET)54 POST (javax.ws.rs.POST)40 PUT (javax.ws.rs.PUT)35 HashMap (java.util.HashMap)34 ArrayList (java.util.ArrayList)32 IOException (java.io.IOException)30 ApiOperation (io.swagger.annotations.ApiOperation)29 ApiResponses (io.swagger.annotations.ApiResponses)29 Response (javax.ws.rs.core.Response)28 Adapter (nl.nn.adapterframework.core.Adapter)21 DELETE (javax.ws.rs.DELETE)19 WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)19 LinkedHashMap (java.util.LinkedHashMap)16 Locale (java.util.Locale)16 Map (java.util.Map)12 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)12