Example #1
0
  public static boolean addHandler(Registry configSystemRegistry, String payload)
      throws RegistryException, XMLStreamException {
    String name;
    OMElement element = AXIOMUtil.stringToOM(payload);
    if (element != null) {
      name = element.getAttributeValue(new QName("class"));
    } else return false;

    if (isHandlerNameInUse(name))
      throw new RegistryException("The added handler name is already in use!");

    String path = getContextRoot() + name;
    Resource resource;
    if (!handlerExists(configSystemRegistry, name)) {
      resource = new ResourceImpl();
    } else {
      throw new RegistryException("The added handler name is already in use!");
    }
    resource.setContent(payload);
    try {
      configSystemRegistry.beginTransaction();
      configSystemRegistry.put(path, resource);
      generateHandler(configSystemRegistry, path);
      configSystemRegistry.commitTransaction();
    } catch (Exception e) {
      configSystemRegistry.rollbackTransaction();
      throw new RegistryException("Unable to generate handler", e);
    }
    return true;
  }
  /**
   * Creates endpoint element for REST service
   *
   * @param requestContext information about current request.
   * @param wadlElement wadl document.
   * @param version wadl version.
   * @return Endpoint Path.
   * @throws RegistryException If fails to create endpoint element.
   */
  private String createEndpointElement(
      RequestContext requestContext, OMElement wadlElement, String version)
      throws RegistryException {
    OMNamespace wadlNamespace = wadlElement.getNamespace();
    String wadlNamespaceURI = wadlNamespace.getNamespaceURI();
    String wadlNamespacePrefix = wadlNamespace.getPrefix();
    OMElement resourcesElement =
        wadlElement.getFirstChildWithName(
            new QName(wadlNamespaceURI, "resources", wadlNamespacePrefix));
    if (resourcesElement != null) {
      String endpointUrl = resourcesElement.getAttributeValue(new QName("base"));
      if (endpointUrl != null) {
        String endpointPath = EndpointUtils.deriveEndpointFromUrl(endpointUrl);
        String endpointName = EndpointUtils.deriveEndpointNameFromUrl(endpointUrl);
        String endpointContent =
            EndpointUtils.getEndpointContentWithOverview(
                endpointUrl, endpointPath, endpointName, version);
        OMElement endpointElement;
        try {
          endpointElement = AXIOMUtil.stringToOM(endpointContent);
        } catch (XMLStreamException e) {
          throw new RegistryException("Error in creating the endpoint element. ", e);
        }

        return RESTServiceUtils.addEndpointToRegistry(
            requestContext, endpointElement, endpointPath);
      } else {
        log.warn("Base path does not exist. endpoint creation may fail. ");
      }
    } else {
      log.warn("Resources element is null. ");
    }
    return null;
  }
Example #3
0
  public static HandlerConfigurationBean deserializeHandlerConfiguration(
      OMElement configurationElement) {
    if (configurationElement == null || !"handler".equals(configurationElement.getLocalName())) {
      return null;
    }
    HandlerConfigurationBean handlerConfigurationBean = new HandlerConfigurationBean();
    handlerConfigurationBean.setHandlerClass(
        configurationElement.getAttributeValue(new QName("class")));
    handlerConfigurationBean.setTenant(configurationElement.getAttributeValue(new QName("tenant")));
    String methodsAttribute = configurationElement.getAttributeValue(new QName("methods"));
    if (methodsAttribute != null && methodsAttribute.length() != 0) {
      handlerConfigurationBean.setMethods(methodsAttribute.split(","));
    }
    @SuppressWarnings("unchecked")
    Iterator<OMElement> handlerProps =
        configurationElement.getChildrenWithName(new QName("property"));
    while (handlerProps.hasNext()) {
      OMElement propElement = handlerProps.next();
      String propName = propElement.getAttributeValue(new QName("name"));
      String propType = propElement.getAttributeValue(new QName("type"));

      if (propType != null && "xml".equals(propType)) {
        handlerConfigurationBean.getXmlProperties().put(propName, propElement);
      } else {
        handlerConfigurationBean.getNonXmlProperties().put(propName, propElement.getText());
      }
      handlerConfigurationBean.getPropertyList().add(propName);
    }
    FilterConfigurationBean filterConfigurationBean = new FilterConfigurationBean();
    OMElement filterElement = configurationElement.getFirstChildWithName(new QName("filter"));
    filterConfigurationBean.setFilterClass(filterElement.getAttributeValue(new QName("class")));
    @SuppressWarnings("unchecked")
    Iterator<OMElement> filterProps = filterElement.getChildrenWithName(new QName("property"));
    while (filterProps.hasNext()) {
      OMElement propElement = filterProps.next();
      String propName = propElement.getAttributeValue(new QName("name"));
      String propType = propElement.getAttributeValue(new QName("type"));

      if (propType != null && "xml".equals(propType)) {
        filterConfigurationBean.getXmlProperties().put(propName, propElement);
      } else {
        filterConfigurationBean.getNonXmlProperties().put(propName, propElement.getText());
      }
      filterConfigurationBean.getPropertyList().add(propName);
    }
    handlerConfigurationBean.setFilter(filterConfigurationBean);
    return handlerConfigurationBean;
  }
Example #4
0
  public static boolean updateHandler(Registry configSystemRegistry, String oldName, String payload)
      throws RegistryException, XMLStreamException {
    if (isHandlerNameInUse(oldName))
      throw new RegistryException("Could not update handler since it is already in use!");

    String newName = null;
    OMElement element = AXIOMUtil.stringToOM(payload);
    if (element != null) {
      newName = element.getAttributeValue(new QName("class"));
    }

    if (newName == null || newName.equals("")) return false; // invalid configuration

    if (oldName == null || oldName.equals("")) {
      String path = getContextRoot() + newName;
      Resource resource;
      if (handlerExists(configSystemRegistry, newName)) {
        return false; // we are adding a new handler
      } else {
        resource = new ResourceImpl();
      }
      resource.setContent(payload);
      try {
        configSystemRegistry.beginTransaction();
        configSystemRegistry.put(path, resource);
        generateHandler(configSystemRegistry, path);
        configSystemRegistry.commitTransaction();
      } catch (Exception e) {
        configSystemRegistry.rollbackTransaction();
        throw new RegistryException("Unable to generate handler", e);
      }
      return true;
    }

    if (newName.equals(oldName)) {
      // updating the rest of the content
      String oldPath = getContextRoot() + oldName;
      Resource resource;
      if (handlerExists(configSystemRegistry, oldName)) {
        resource = configSystemRegistry.get(oldPath);
      } else {
        resource = new ResourceImpl(); // will this ever happen?
      }
      resource.setContent(payload);
      try {
        configSystemRegistry.beginTransaction();
        removeHandler(configSystemRegistry, oldName);
        configSystemRegistry.put(oldPath, resource);
        generateHandler(configSystemRegistry, oldPath);
        configSystemRegistry.commitTransaction();
      } catch (Exception e) {
        configSystemRegistry.rollbackTransaction();
        throw new RegistryException("Unable to generate handler", e);
      }
      return true;
    } else {
      String oldPath = getContextRoot() + oldName;
      String newPath = getContextRoot() + newName;

      if (handlerExists(configSystemRegistry, newName)) {
        return false; // we are trying to use the name of a existing handler
      }

      Resource resource;
      if (handlerExists(configSystemRegistry, oldName)) {
        resource = configSystemRegistry.get(oldPath);
      } else {
        resource = new ResourceImpl(); // will this ever happen?
      }

      resource.setContent(payload);
      try {
        configSystemRegistry.beginTransaction();
        configSystemRegistry.put(newPath, resource);
        generateHandler(configSystemRegistry, newPath);
        removeHandler(configSystemRegistry, oldName);
        configSystemRegistry.delete(oldPath);
        configSystemRegistry.commitTransaction();
      } catch (Exception e) {
        configSystemRegistry.rollbackTransaction();
        throw new RegistryException("Unable to renew handler", e);
      }
      return true;
    }
  }