Search in sources :

Example 11 with ApiMethod

use of org.entando.entando.aps.system.services.api.model.ApiMethod in project entando-core by entando.

the class ApiServiceAction method save.

/**
 * Save an api service.
 *
 * @return The result code.
 */
public String save() {
    try {
        String key = this.getServiceKey().trim();
        ApiMethod masterMethod = this.getMethod(this.getNamespace(), this.getResourceName());
        String[] freeParams = null;
        if (null != this.getFreeParameters()) {
            freeParams = new String[this.getFreeParameters().size()];
            for (int i = 0; i < this.getFreeParameters().size(); i++) {
                freeParams[i] = this.getFreeParameters().get(i);
            }
        }
        ApiService service = new ApiService(key, this.getDescriptions(), masterMethod, this.getApiParameterValues(), freeParams, this.getTag(), !this.isHiddenService(), this.isActiveService());
        service.setRequiredAuth(this.getRequiredAuth());
        if (null != this.getRequiredGroup() && this.getRequiredGroup().trim().length() > 0) {
            service.setRequiredGroup(this.getRequiredGroup());
        }
        if (null != this.getRequiredPermission() && this.getRequiredPermission().trim().length() > 0) {
            service.setRequiredPermission(this.getRequiredPermission());
        }
        this.getApiCatalogManager().saveService(service);
    } catch (Throwable t) {
        _logger.error("error in save", t);
        return FAILURE;
    }
    return SUCCESS;
}
Also used : ApiService(org.entando.entando.aps.system.services.api.model.ApiService) ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod)

Example 12 with ApiMethod

use of org.entando.entando.aps.system.services.api.model.ApiMethod in project entando-core by entando.

the class SchemaGeneratorActionHelper method extractReturnType.

protected Class extractReturnType(ApiMethod method, HttpServletRequest request) throws ApsSystemException {
    Class returnType = null;
    try {
        Object bean = ApsWebApplicationUtils.getBean(method.getSpringBean(), request);
        if (null == bean) {
            throw new ApsSystemException("Null bean '" + method.getSpringBean() + "' from method " + method.getHttpMethod() + " of resource " + method.getResourceName());
        }
        Class beanClass = bean.getClass();
        String methodName = method.getSpringBeanMethod();
        Method beanMethod = null;
        if (method.getHttpMethod().equals(ApiMethod.HttpMethod.GET) || method.getHttpMethod().equals(ApiMethod.HttpMethod.DELETE)) {
            Class[] parameterTypes = new Class[] { Properties.class };
            beanMethod = beanClass.getDeclaredMethod(methodName, parameterTypes);
        } else {
            Class expectedType = method.getExpectedType();
            if (null == expectedType) {
                throw new ApsSystemException("Null expectedType for Method " + method.getHttpMethod() + " for resource " + method.getResourceName());
            }
            try {
                // special case - put or post method with properties
                Class[] parameterTypes = new Class[] { expectedType, Properties.class };
                beanMethod = beanClass.getDeclaredMethod(methodName, parameterTypes);
            } catch (Exception e) {
            // nothing to catch
            }
            if (null == beanMethod) {
                Class[] parameterTypes2 = new Class[] { expectedType };
                beanMethod = beanClass.getDeclaredMethod(methodName, parameterTypes2);
            }
        }
        if (null != beanMethod) {
            returnType = beanMethod.getReturnType();
        }
        if (null != returnType && returnType.getName().equals("void")) {
            return null;
        }
    } catch (Throwable t) {
        _logger.error("Error extracting return type", t);
        // ApsSystemUtils.logThrowable(t, this, "extractReturnType", "Error extracting return type ");
        return null;
    }
    return returnType;
}
Also used : ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException) ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod) Method(java.lang.reflect.Method) Properties(java.util.Properties) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException)

Example 13 with ApiMethod

use of org.entando.entando.aps.system.services.api.model.ApiMethod in project entando-core by entando.

the class ApiServiceFinderAction method buildServiceGroups.

private void buildServiceGroups(Map<String, List<ApiSelectItem>> groups) throws Throwable {
    try {
        Map<String, ApiService> serviceMap = this.getApiCatalogManager().getServices();
        if (null == serviceMap || serviceMap.isEmpty())
            return;
        Iterator<ApiService> services = serviceMap.values().iterator();
        while (services.hasNext()) {
            ApiService apiService = services.next();
            if (this.includeServiceIntoMapping(apiService)) {
                ApiMethod masterMethod = apiService.getMaster();
                String pluginCode = masterMethod.getPluginCode();
                if (null != pluginCode && pluginCode.trim().length() > 0) {
                    this.addService(pluginCode, apiService, groups);
                } else if (masterMethod.getSource().equals("core")) {
                    this.addService(masterMethod.getSource(), apiService, groups);
                } else {
                    this.addService("custom", apiService, groups);
                }
            }
        }
    } catch (Throwable t) {
        _logger.error("error in buildServiceGroups", t);
        // ApsSystemUtils.logThrowable(t, this, "buildServiceGroups");
        throw t;
    }
}
Also used : ApiService(org.entando.entando.aps.system.services.api.model.ApiService) ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod)

Example 14 with ApiMethod

use of org.entando.entando.aps.system.services.api.model.ApiMethod in project entando-core by entando.

the class ResponseBuilder method invokeDeleteMethod.

private Object invokeDeleteMethod(ApiMethod api, Object bean, Properties parameters) throws NoSuchMethodException, InvocationTargetException, Throwable {
    Class[] parameterTypes = new Class[] { Properties.class };
    Class beanClass = bean.getClass();
    String methodName = api.getSpringBeanMethod();
    Method method = beanClass.getDeclaredMethod(methodName, parameterTypes);
    return method.invoke(bean, parameters);
}
Also used : ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod) Method(java.lang.reflect.Method) Properties(java.util.Properties)

Example 15 with ApiMethod

use of org.entando.entando.aps.system.services.api.model.ApiMethod in project entando-core by entando.

the class ResponseBuilder method invokeGetMethod.

protected Object invokeGetMethod(ApiMethod apiMethod, Object bean, String methodSuffix, Properties parameters, boolean throwException) throws ApiException, Throwable {
    String methodName = null;
    Object result = null;
    try {
        Class[] parameterTypes = new Class[] { Properties.class };
        Class beanClass = bean.getClass();
        methodName = (null != methodSuffix) ? apiMethod.getSpringBeanMethod() + methodSuffix.trim() : apiMethod.getSpringBeanMethod();
        Method method = beanClass.getDeclaredMethod(methodName, parameterTypes);
        result = method.invoke(bean, parameters);
    } catch (NoSuchMethodException e) {
        if (throwException) {
            _logger.error("No such method '{}' of class '{}'", methodName, bean.getClass(), e);
            throw new ApiException(IApiErrorCodes.API_METHOD_ERROR, "Method not supported - " + this.buildApiSignature(apiMethod), Response.Status.INTERNAL_SERVER_ERROR);
        }
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof ApiException) {
            throw (ApiException) e.getTargetException();
        } else if (throwException) {
            _logger.error("Error invoking method '{}' of class '{}'", methodName, bean.getClass());
            throw new ApiException(IApiErrorCodes.API_METHOD_ERROR, "Error invoking Method - " + this.buildApiSignature(apiMethod), Response.Status.INTERNAL_SERVER_ERROR);
        }
    } catch (Throwable t) {
        if (throwException) {
            _logger.error("Error invoking method - {} {} of class '{}'", this.buildApiSignature(apiMethod), methodName, bean.getClass(), t);
            throw t;
        }
    }
    return result;
}
Also used : ApiMethod(org.entando.entando.aps.system.services.api.model.ApiMethod) Method(java.lang.reflect.Method) Properties(java.util.Properties) InvocationTargetException(java.lang.reflect.InvocationTargetException) ApiException(org.entando.entando.aps.system.services.api.model.ApiException)

Aggregations

ApiMethod (org.entando.entando.aps.system.services.api.model.ApiMethod)40 Properties (java.util.Properties)11 ApiResource (org.entando.entando.aps.system.services.api.model.ApiResource)10 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)9 ApsProperties (com.agiletec.aps.util.ApsProperties)6 StringApiResponse (org.entando.entando.aps.system.services.api.model.StringApiResponse)6 ApiException (org.entando.entando.aps.system.services.api.model.ApiException)5 ApiService (org.entando.entando.aps.system.services.api.model.ApiService)5 Method (java.lang.reflect.Method)4 HashMap (java.util.HashMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 ApiMethodRelatedWidget (org.entando.entando.aps.system.services.api.model.ApiMethodRelatedWidget)3 EntitySearchFilter (com.agiletec.aps.system.common.entity.model.EntitySearchFilter)2 AttributeInterface (com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2