@Override
  public <T> Action getAction(T input) {
    if (!canHandle(input)) {
      return null;
    }

    Metacard metacard = (Metacard) input;

    if (StringUtils.isBlank(metacard.getId())) {
      LOGGER.info("No id given. No action to provide.");
      return null;
    }

    if (isHostUnset(SystemBaseUrl.getHost())) {
      LOGGER.info("Host name/ip not set. Cannot create link for metacard.");
      return null;
    }

    String metacardId = null;
    String metacardSource = null;

    try {
      metacardId = URLEncoder.encode(metacard.getId(), CharEncoding.UTF_8);
      metacardSource = URLEncoder.encode(getSource(metacard), CharEncoding.UTF_8);
    } catch (UnsupportedEncodingException e) {
      LOGGER.info("Unsupported Encoding exception", e);
      return null;
    }

    return getAction(metacardId, metacardSource);
  }
Esempio n. 2
0
public class SystemPropertiesAdmin implements SystemPropertiesAdminMBean {

  private MBeanServer mbeanServer;

  private ObjectName objectName;

  private String oldHostName = SystemBaseUrl.getHost();

  private static final String KARAF_ETC = "karaf.etc";

  private static final String SYSTEM_PROPERTIES_FILE = "system.properties";

  private static final String USERS_PROPERTIES_FILE = "users.properties";

  private static final String USERS_ATTRIBUTES_FILE = "users.attributes";

  private static final String HOST_TITLE = "Host";

  private static final String HOST_DESCRIPTION =
      "The host name or IP address used to advertise the system. Possibilities include the address of a single node of that of a load balancer in a multi-node deployment. NOTE: Does not change the address the system runs on.";

  private static final String PROTOCOL_TITLE = "Default Protocol";

  private static final String PROTOCOL_DESCRIPTION =
      "The protocol used to advertise the system. When selecting the protocol, be sure to enter the port number corresponding to that protocol.";

  private static final ArrayList<String> PROTOCOL_OPTIONS = new ArrayList<>();

  static {
    PROTOCOL_OPTIONS.add("https://");
    PROTOCOL_OPTIONS.add("http://");
  }

  private static final String DEFAULT_PORT_TITLE = "Default Port";

  private static final String DEFAULT_PORT_DESCRIPTION =
      "The default port used to advertise the system. Possibilities include the port of a single node of that of a load balancer in a multi-node deployment. NOTE: Does not change the port the system runs on.";

  private static final String HTTP_PORT_TITLE = "HTTP Port";

  private static final String HTTP_PORT_DESCRIPTION =
      "The port used to advertise the system. Possibilities include the port of a single node of that of a load balancer in a multi-node deployment. NOTE: Does not change the port the system runs on.";

  private static final String HTTPS_PORT_TITLE = "HTTPS Port";

  private static final String HTTPS_PORT_DESCRIPTION =
      "The secure port used to advertise the system. Possibilities include the port of a single node of that of a load balancer in a multi-node deployment. NOTE: Does not change the port the system runs on.";

  private static final String ORGANIZATION_TITLE = "Organization";

  private static final String ORGANIZATION_DESCRIPTION =
      "The name of the organization that runs this instance.";

  private static final String SITE_CONTACT_TITLE = "Site Contact";

  private static final String SITE_CONTACT_DESCRIPTION = "The email address of the site contact.";

  private static final String SITE_NAME_TITLE = "Site Name";

  private static final String SITE_NAME_DESCRIPTION =
      "The unique name of this instance. This name will be provided via web services that ask for the name.";

  private static final String VERSION_TITLE = "Version";

  private static final String VERSION_DESCRIPTION = "The version of this instance.";

  private static final Logger LOGGER = LoggerFactory.getLogger(SystemPropertiesAdmin.class);

  private static final ObjectMapper MAPPER = JsonFactory.create();

  public SystemPropertiesAdmin() {
    configureMBean();
  }

  @Override
  public List<SystemPropertyDetails> readSystemProperties() {
    LOGGER.info("get system properties");

    ArrayList<SystemPropertyDetails> properties = new ArrayList<>();
    properties.add(
        getSystemPropertyDetails(
            SystemBaseUrl.PROTOCOL, PROTOCOL_TITLE, PROTOCOL_DESCRIPTION, PROTOCOL_OPTIONS));
    properties.add(
        getSystemPropertyDetails(SystemBaseUrl.HOST, HOST_TITLE, HOST_DESCRIPTION, null));
    properties.add(
        getSystemPropertyDetails(
            SystemBaseUrl.PORT, DEFAULT_PORT_TITLE, DEFAULT_PORT_DESCRIPTION, null));
    properties.add(
        getSystemPropertyDetails(
            SystemBaseUrl.HTTP_PORT, HTTP_PORT_TITLE, HTTP_PORT_DESCRIPTION, null));
    properties.add(
        getSystemPropertyDetails(
            SystemBaseUrl.HTTPS_PORT, HTTPS_PORT_TITLE, HTTPS_PORT_DESCRIPTION, null));
    properties.add(
        getSystemPropertyDetails(
            SystemInfo.ORGANIZATION, ORGANIZATION_TITLE, ORGANIZATION_DESCRIPTION, null));
    properties.add(
        getSystemPropertyDetails(
            SystemInfo.SITE_CONTACT, SITE_CONTACT_TITLE, SITE_CONTACT_DESCRIPTION, null));
    properties.add(
        getSystemPropertyDetails(
            SystemInfo.SITE_NAME, SITE_NAME_TITLE, SITE_NAME_DESCRIPTION, null));
    properties.add(
        getSystemPropertyDetails(SystemInfo.VERSION, VERSION_TITLE, VERSION_DESCRIPTION, null));

    return properties;
  }

  @Override
  public void writeSystemProperties(Map<String, String> updatedSystemProperties) {
    if (updatedSystemProperties == null) {
      return;
    }
    // Get system.properties file
    // save off the current/old hostname before we make any changes
    oldHostName = SystemBaseUrl.getHost();

    String etcDir = System.getProperty(KARAF_ETC);
    String systemPropertyFilename = etcDir + File.separator + SYSTEM_PROPERTIES_FILE;
    String userPropertiesFilename = etcDir + File.separator + USERS_PROPERTIES_FILE;
    String userAttributesFilename = etcDir + File.separator + USERS_ATTRIBUTES_FILE;

    File systemPropertiesFile = new File(systemPropertyFilename);
    File userPropertiesFile = new File(userPropertiesFilename);
    File userAttributesFile = new File(userAttributesFilename);

    try {
      Properties systemDotProperties = new Properties(systemPropertiesFile);

      updateProperty(SystemBaseUrl.PORT, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemBaseUrl.HOST, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemBaseUrl.PROTOCOL, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemBaseUrl.HTTP_PORT, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemBaseUrl.HTTPS_PORT, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemInfo.ORGANIZATION, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemInfo.SITE_CONTACT, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemInfo.SITE_NAME, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemInfo.VERSION, updatedSystemProperties, systemDotProperties);

      systemDotProperties.save();

    } catch (IOException e) {
      LOGGER.warn("Exception while writing to system.properties file.", e);
    }

    try {
      Properties userDotProperties = new Properties(userPropertiesFile);

      if (!userDotProperties.isEmpty()) {
        String oldHostValue = userDotProperties.getProperty(oldHostName);

        if (oldHostValue != null) {
          userDotProperties.remove(oldHostName);
          userDotProperties.setProperty(System.getProperty(SystemBaseUrl.HOST), oldHostValue);

          userDotProperties.save();
        }
      }

    } catch (IOException e) {
      LOGGER.warn("Exception while writing to users.properties file.", e);
    }

    Map<String, Object> json = null;
    try (InputStream stream = Files.newInputStream(Paths.get(userAttributesFile.toURI()))) {
      json = MAPPER.parser().parseMap(stream);
      if (json.containsKey(oldHostName)) {
        json.put(System.getProperty(SystemBaseUrl.HOST), json.remove(oldHostName));
      }
    } catch (IOException e) {
      LOGGER.error("Unable to read system user attribute file for hostname update.", e);
    }

    if (json != null) {
      try (OutputStream stream = Files.newOutputStream(Paths.get(userAttributesFile.toURI()))) {
        MAPPER.writeValue(stream, json);
      } catch (IOException e) {
        LOGGER.error("Unable to write system user attribute file for hostname update.", e);
      }
    }
  }

  private SystemPropertyDetails getSystemPropertyDetails(
      String key, String title, String description, List<String> options) {
    String property = System.getProperty(key, "");
    return new SystemPropertyDetails(title, description, options, key, property);
  }

  private void updateProperty(
      String key, Map<String, String> updatedProperties, Properties systemDotProperties) {
    if (updatedProperties.containsKey(key)) {
      String value = updatedProperties.get(key);
      systemDotProperties.put(key, value);
      System.setProperty(key, value);
    }
  }

  private void configureMBean() {
    mbeanServer = ManagementFactory.getPlatformMBeanServer();

    try {
      objectName = new ObjectName(SystemPropertiesAdminMBean.OBJECT_NAME);
    } catch (MalformedObjectNameException e) {
      LOGGER.warn(
          "Exception while creating object name: " + SystemPropertiesAdminMBean.OBJECT_NAME, e);
    }

    try {
      try {
        mbeanServer.registerMBean(
            new StandardMBean(this, SystemPropertiesAdminMBean.class), objectName);
      } catch (InstanceAlreadyExistsException e) {
        mbeanServer.unregisterMBean(objectName);
        mbeanServer.registerMBean(
            new StandardMBean(this, SystemPropertiesAdminMBean.class), objectName);
      }
    } catch (Exception e) {
      LOGGER.error("Could not register mbean.", e);
    }
  }

  public void shutdown() {
    try {
      if (objectName != null && mbeanServer != null) {
        mbeanServer.unregisterMBean(objectName);
      }
    } catch (Exception e) {
      LOGGER.warn("Exception unregistering mbean: ", e);
      throw new RuntimeException(e);
    }
  }
}
Esempio n. 3
0
  @Override
  public void writeSystemProperties(Map<String, String> updatedSystemProperties) {
    if (updatedSystemProperties == null) {
      return;
    }
    // Get system.properties file
    // save off the current/old hostname before we make any changes
    oldHostName = SystemBaseUrl.getHost();

    String etcDir = System.getProperty(KARAF_ETC);
    String systemPropertyFilename = etcDir + File.separator + SYSTEM_PROPERTIES_FILE;
    String userPropertiesFilename = etcDir + File.separator + USERS_PROPERTIES_FILE;
    String userAttributesFilename = etcDir + File.separator + USERS_ATTRIBUTES_FILE;

    File systemPropertiesFile = new File(systemPropertyFilename);
    File userPropertiesFile = new File(userPropertiesFilename);
    File userAttributesFile = new File(userAttributesFilename);

    try {
      Properties systemDotProperties = new Properties(systemPropertiesFile);

      updateProperty(SystemBaseUrl.PORT, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemBaseUrl.HOST, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemBaseUrl.PROTOCOL, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemBaseUrl.HTTP_PORT, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemBaseUrl.HTTPS_PORT, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemInfo.ORGANIZATION, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemInfo.SITE_CONTACT, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemInfo.SITE_NAME, updatedSystemProperties, systemDotProperties);
      updateProperty(SystemInfo.VERSION, updatedSystemProperties, systemDotProperties);

      systemDotProperties.save();

    } catch (IOException e) {
      LOGGER.warn("Exception while writing to system.properties file.", e);
    }

    try {
      Properties userDotProperties = new Properties(userPropertiesFile);

      if (!userDotProperties.isEmpty()) {
        String oldHostValue = userDotProperties.getProperty(oldHostName);

        if (oldHostValue != null) {
          userDotProperties.remove(oldHostName);
          userDotProperties.setProperty(System.getProperty(SystemBaseUrl.HOST), oldHostValue);

          userDotProperties.save();
        }
      }

    } catch (IOException e) {
      LOGGER.warn("Exception while writing to users.properties file.", e);
    }

    Map<String, Object> json = null;
    try (InputStream stream = Files.newInputStream(Paths.get(userAttributesFile.toURI()))) {
      json = MAPPER.parser().parseMap(stream);
      if (json.containsKey(oldHostName)) {
        json.put(System.getProperty(SystemBaseUrl.HOST), json.remove(oldHostName));
      }
    } catch (IOException e) {
      LOGGER.error("Unable to read system user attribute file for hostname update.", e);
    }

    if (json != null) {
      try (OutputStream stream = Files.newOutputStream(Paths.get(userAttributesFile.toURI()))) {
        MAPPER.writeValue(stream, json);
      } catch (IOException e) {
        LOGGER.error("Unable to write system user attribute file for hostname update.", e);
      }
    }
  }