Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some logic into JeevesNodeAwareLogoutSuccessHandler to deal with the case where no port is set in the settings manager #7124

Merged

Conversation

archaeogeek
Copy link
Contributor

This PR handles the error on signout where there is no port set in the settings manager. Previously if no port was set (because it's not needed) logging out would produce a null pointer error (see below):

Screenshot from 2023-05-31 12-08-13

This PR adds some logic to check for the value of the port in the settings and to set it to 80 if it is not included.
With this pull request, if no port is set the logout error no longer occurs.

…h the case where no port is set in the settings manager
@archaeogeek archaeogeek requested a review from josegar74 May 31, 2023 11:10
@josegar74 josegar74 added this to the 4.2.5 milestone Jun 1, 2023
@archaeogeek
Copy link
Contributor Author

@josegar74 thanks for the suggested changes, I have made them and re-tested

@josegar74
Copy link
Member

@archaeogeek the code works fine, but I'm checking additional code and setting the port to empty it can cause issues in other code like this one, if the value is empty it will add the : character:

String port = settingManager.getValue(Settings.SYSTEM_SERVER_PORT);
if (port.equals("80")) {
port = "";
} else {
port = ":" + port;
}

Note also that xslt process code can have the same problem as the previous class:

<xsl:variable name="port"> <xsl:value-of select="$env/system/server/port"/></xsl:variable>
<xsl:variable name="url"
select="concat($env/system/server/protocol, '://',
$env/system/server/host,
if (($env/system/server/protocol = 'http' and $port = '80') or
($env/system/server/protocol = 'https' and $port = '443')) then '' else concat(':', $port),
/root/gui/url)"/>

I would do the following:

  1. Change https://github.com/geonetwork/core-geonetwork/blob/main/core/src/main/java/org/fao/geonet/kernel/setting/SettingInfo.java to add this method:
    public int getSitePort() {
        SettingManager settingManager = ApplicationContextHolder.get().getBean(SettingManager.class);

        // some conditional logic to handle the case where there's no port in the settings
        int sitePort = Geonet.DefaultHttpPort.HTTP;
        if (StringUtils.isNumeric(settingManager.getValue(Settings.SYSTEM_SERVER_PORT))) {
            sitePort = settingManager.getValueAsInt(Settings.SYSTEM_SERVER_PORT);
        }

        return sitePort;
    }

And use it in your change of JeevesNodeAwareLogoutSuccessHandler.java and in BaseMetadataManager.java

@archaeogeek
Copy link
Contributor Author

@josegar74 I've tried to do what you suggested, but have ended up with compilation errors: Here's what I've done:

Add the following after getSiteUrl in SettingInfo.java, along with adding a line import org.apache.commons.lang3.StringUtils;:

 /**
   * Handle the case where the port in Settings is empty
  */

    public Integer getSitePort() {
             SettingManager settingManager = ApplicationContextHolder.get().getBean(SettingManager.class);

        // some conditional logic to handle the case where there's no port in the settings
        int sitePort = Geonet.DefaultHttpPort.HTTP;
        if (StringUtils.isNumeric(settingManager.getValue(Settings.SYSTEM_SERVER_PORT))) {
            sitePort = settingManager.getValueAsInt(Settings.SYSTEM_SERVER_PORT);
        }

        return sitePort;
    }

Change BaseMetadataManager.java like so (eg by commenting out the logic for picking up the port and adding the new line), and adding the line import org.fao.geonet.kernel.setting.SettingInfo;:

String protocol = settingManager.getValue(Settings.SYSTEM_SERVER_PROTOCOL);
String host = settingManager.getValue(Settings.SYSTEM_SERVER_HOST);
//String port = settingManager.getValue(Settings.SYSTEM_SERVER_PORT);
       //if (port.equals("80")) {
        //    port = "";
        //} else {
        //    port = ":" + port;
        //}
String port = Integer.toString(SettingInfo.getSitePort());

Changing JeevesNodeAwareLogoutSuccessHanlder.java by adding the line import org.fao.geonet.kernel.setting.SettingInfo; and changing the lines to get the port:

// some conditional logic to handle the case where there's no port in the settings
//int sitePort = Geonet.DefaultHttpPort.HTTP;
//if (StringUtils.isNumeric(settingManager.getValue(Settings.SYSTEM_SERVER_PORT))) {
//    sitePort = settingManager.getValueAsInt(Settings.SYSTEM_SERVER_PORT);
// }

int sitePort = SettingInfo.getSitePort(); 

When I then build with maven I get the following error:

[ERROR] /home/jo/Git/core-geonetwork/core/src/main/java/org/fao/geonet/kernel/datamanager/base/BaseMetadataManager.java:[927,51] non-static method getSitePort() cannot be referenced from a static context
[ERROR] /home/jo/Git/core-geonetwork/core/src/main/java/jeeves/config/springutil/JeevesNodeAwareLogoutSuccessHandler.java:[98,47] non-static method getSitePort() cannot be referenced from a s
tatic context

Can you see what I'm doing wrong?

@archaeogeek
Copy link
Contributor Author

@josegar74 could you check my code to see where I am going wrong?

…goutSuccessHandler and BaseMetadataManager to get around compilation error about not referring to a non-static method from a static context
@archaeogeek
Copy link
Contributor Author

@josegar74 Hopefully these latest changes fix the issues you reported- I'd be grateful if this could make it into the 4.2.5 release if possible!

@josegar74
Copy link
Member

@archaeogeek I'm going to check the code, thanks and apologies for the late reply, missing your previous comments to review the code.

Copy link
Member

@josegar74 josegar74 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@archaeogeek I've added some code suggestions, to remove commented code and fix a wrong tab in a code line. You can commit them from GitHub.

Other than that, looks working ok, thanks.

Jo Cook and others added 3 commits June 27, 2023 08:57
Co-authored-by: Jose García <josegar74@gmail.com>
…MetadataManager.java

Co-authored-by: Jose García <josegar74@gmail.com>
…outSuccessHandler.java

Co-authored-by: Jose García <josegar74@gmail.com>
@archaeogeek
Copy link
Contributor Author

@josegar74 Great, is it OK to merge this now?

@archaeogeek archaeogeek merged commit 67960df into geonetwork:main Jul 5, 2023
josegar74 added a commit to GeoCat/core-geonetwork that referenced this pull request Jul 5, 2023
fxprunayre added a commit that referenced this pull request Jul 6, 2023
…uplication (#7200)

* Additional code refactor related to #7124 to avoid code duplication

* Update core/src/main/java/org/fao/geonet/kernel/setting/SettingManager.java

Co-authored-by: François Prunayre <fx.prunayre@gmail.com>

---------

Co-authored-by: François Prunayre <fx.prunayre@gmail.com>
@archaeogeek archaeogeek deleted the jeevesnodeawarelogout-port-fix branch September 1, 2023 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants