فهرست منبع

add version publish service

Jason Rivard 3 سال پیش
والد
کامیت
1461544798

+ 1 - 1
data-service/pom.xml

@@ -126,7 +126,7 @@
                                 <appRoot>/usr/local/tomcat/webapps/ROOT</appRoot>
                                 <jvmFlags>
                                     <jvmFlag>-server</jvmFlag>
-                                    <jvmFlag>-Xmx512m</jvmFlag>
+                                    <jvmFlag>-Xmx256m</jvmFlag>
                                 </jvmFlags>
                                 <environment>
                                     <DATA_SERVICE_PROPS>/config/data-service.properties</DATA_SERVICE_PROPS>

+ 53 - 0
data-service/src/main/java/password/pwm/receiver/PublishVersionServlet.java

@@ -0,0 +1,53 @@
+/*
+ * Password Management Servlets (PWM)
+ * http://www.pwm-project.org
+ *
+ * Copyright (c) 2006-2009 Novell, Inc.
+ * Copyright (c) 2009-2021 The PWM Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package password.pwm.receiver;
+
+import password.pwm.bean.pub.PublishVersionBean;
+import password.pwm.ws.server.RestResultBean;
+
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Collections;
+
+@WebServlet(
+        urlPatterns = {
+                "/version",
+        }
+)
+public class PublishVersionServlet extends HttpServlet
+{
+    @Override
+    protected void doGet( final HttpServletRequest req, final HttpServletResponse resp )
+            throws IOException
+    {
+        final ContextManager contextManager = ContextManager.getContextManager( req.getServletContext() );
+        final PwmReceiverApp app = contextManager.getApp();
+        final PublishVersionBean publishVersionBean = new PublishVersionBean(
+                Collections.singletonMap( PublishVersionBean.VersionKey.current, app.getSettings().getCurrentVersionInfo() ) );
+
+        final RestResultBean<PublishVersionBean> restResultBean = RestResultBean.withData( publishVersionBean, PublishVersionBean.class );
+
+        ReceiverUtil.outputJsonResponse( req, resp, restResultBean );
+    }
+}

+ 56 - 0
data-service/src/main/java/password/pwm/receiver/ReceiverUtil.java

@@ -0,0 +1,56 @@
+/*
+ * Password Management Servlets (PWM)
+ * http://www.pwm-project.org
+ *
+ * Copyright (c) 2006-2009 Novell, Inc.
+ * Copyright (c) 2009-2021 The PWM Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package password.pwm.receiver;
+
+import password.pwm.http.PwmHttpRequestWrapper;
+import password.pwm.ws.server.RestResultBean;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class ReceiverUtil
+{
+    static <T> void outputJsonResponse(
+
+            final HttpServletRequest request,
+            final HttpServletResponse response,
+            final RestResultBean<T> restResultBean
+    )
+            throws IOException
+    {
+        final boolean jsonPrettyPrint = PwmHttpRequestWrapper.isPrettyPrintJsonParameterTrue( request );
+        response.setHeader( "Content", "application/json" );
+        response.getWriter().print( restResultBean.toJson( jsonPrettyPrint ) );
+    }
+
+    static int silentIntParser( final String input )
+    {
+        try
+        {
+            return Integer.parseInt( input );
+        }
+        catch ( final NumberFormatException e )
+        {
+            return 0;
+        }
+    }
+}

+ 35 - 3
data-service/src/main/java/password/pwm/receiver/Settings.java

@@ -20,6 +20,7 @@
 
 package password.pwm.receiver;
 
+import password.pwm.bean.VersionNumber;
 import password.pwm.util.java.StringUtil;
 import password.pwm.util.java.TimeDuration;
 
@@ -37,6 +38,8 @@ import java.util.stream.Collectors;
 
 public class Settings
 {
+    private static final Logger LOGGER = Logger.createLogger( Setting.class );
+
     enum Setting
     {
         ftpMode( FtpMode.ftp.name() ),
@@ -45,7 +48,8 @@ public class Settings
         ftpPassword( null ),
         ftpReadPath( null ),
         storagePath( null ),
-        maxInstanceSeconds( Long.toString( TimeDuration.of( 14, TimeDuration.Unit.DAYS ).as( TimeDuration.Unit.SECONDS ) ) ),;
+        maxInstanceSeconds( Long.toString( TimeDuration.of( 14, TimeDuration.Unit.DAYS ).as( TimeDuration.Unit.SECONDS ) ) ),
+        currentVersion( null ),;
 
         private final String defaultValue;
 
@@ -68,9 +72,12 @@ public class Settings
 
     private final Map<Setting, String> settings;
 
+    private final VersionNumber versionNumber;
+
     private Settings( final Map<Setting, String> settings )
     {
         this.settings = settings;
+        this.versionNumber = parseCurrentVersionInfo();
     }
 
     static Settings readFromFile( final String filename ) throws IOException
@@ -82,8 +89,8 @@ public class Settings
             properties.load( reader );
             final Map<Setting, String> returnMap = EnumSet.allOf( Setting.class ).stream()
                     .collect( Collectors.toUnmodifiableMap(
-                      setting -> setting,
-                      setting -> properties.getProperty( setting.name(), setting.getDefaultValue() )
+                            setting -> setting,
+                            setting -> properties.getProperty( setting.name(), setting.getDefaultValue() )
                     ) );
 
             return new Settings( returnMap );
@@ -100,4 +107,29 @@ public class Settings
         final String value = settings.get( Setting.ftpSite );
         return StringUtil.notEmpty( value );
     }
+
+    public VersionNumber getCurrentVersionInfo()
+    {
+        return versionNumber;
+    }
+
+    private VersionNumber parseCurrentVersionInfo()
+    {
+        final String stringVersion = getSetting( Setting.currentVersion );
+
+        if ( stringVersion == null || stringVersion.isEmpty() )
+        {
+            return VersionNumber.ZERO;
+        }
+
+        try
+        {
+            return VersionNumber.parse( stringVersion );
+        }
+        catch ( final Exception e )
+        {
+            LOGGER.info( "error parsing version string from setting properties: " + e.getMessage() );
+            return VersionNumber.ZERO;
+        }
+    }
 }

+ 7 - 8
data-service/src/main/java/password/pwm/receiver/TelemetryRestReceiver.java

@@ -24,9 +24,8 @@ import password.pwm.bean.TelemetryPublishBean;
 import password.pwm.error.ErrorInformation;
 import password.pwm.error.PwmError;
 import password.pwm.error.PwmUnrecoverableException;
-import password.pwm.http.PwmHttpRequestWrapper;
-import password.pwm.i18n.Message;
 import password.pwm.http.ServletUtility;
+import password.pwm.i18n.Message;
 import password.pwm.util.json.JsonFactory;
 import password.pwm.ws.server.RestResultBean;
 
@@ -38,7 +37,6 @@ import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 
 @WebServlet(
-        name = "TelemetryRestReceiver",
         urlPatterns = {
                 "/telemetry",
         }
@@ -50,24 +48,25 @@ public class TelemetryRestReceiver extends HttpServlet
     protected void doPost( final HttpServletRequest req, final HttpServletResponse resp )
             throws ServletException, IOException
     {
-        final boolean jsonPrettyPrint = PwmHttpRequestWrapper.isPrettyPrintJsonParameterTrue( req );
         try
         {
-            resp.setHeader( "Content", "application/json" );
             final String input = ServletUtility.readRequestBodyAsString( req, 1024 * 1024 );
             final TelemetryPublishBean telemetryPublishBean = JsonFactory.get().deserialize( input, TelemetryPublishBean.class );
             final Storage storage = ContextManager.getContextManager( this.getServletContext() ).getApp().getStorage();
             storage.store( telemetryPublishBean );
-            resp.getWriter().print( RestResultBean.forSuccessMessage( null, null, null, Message.Success_Unknown ).toJson( jsonPrettyPrint ) );
+
+            final RestResultBean restResultBean = RestResultBean.forSuccessMessage( null, null, null, Message.Success_Unknown );
+            ReceiverUtil.outputJsonResponse( req, resp, restResultBean );
         }
         catch ( final PwmUnrecoverableException e )
         {
-            resp.getWriter().print( RestResultBean.fromError( e.getErrorInformation() ).toJson( jsonPrettyPrint ) );
+            final RestResultBean restResultBean = RestResultBean.fromError( e.getErrorInformation() );
+            ReceiverUtil.outputJsonResponse( req, resp, restResultBean );
         }
         catch ( final Exception e )
         {
             final RestResultBean restResultBean = RestResultBean.fromError( new ErrorInformation( PwmError.ERROR_INTERNAL, e.getMessage() ) );
-            resp.getWriter().print( restResultBean.toJson( jsonPrettyPrint ) );
+            ReceiverUtil.outputJsonResponse( req, resp, restResultBean );
         }
     }
 }

+ 89 - 0
lib-data/src/main/java/password/pwm/bean/VersionNumber.java

@@ -0,0 +1,89 @@
+/*
+ * Password Management Servlets (PWM)
+ * http://www.pwm-project.org
+ *
+ * Copyright (c) 2006-2009 Novell, Inc.
+ * Copyright (c) 2009-2021 The PWM Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package password.pwm.bean;
+
+import lombok.AccessLevel;
+import lombok.AllArgsConstructor;
+import lombok.Value;
+
+import java.util.Comparator;
+import java.util.Objects;
+import java.util.regex.Pattern;
+
+@Value
+@AllArgsConstructor( access = AccessLevel.PRIVATE )
+public class VersionNumber implements Comparable<VersionNumber>
+{
+    private static final String VERSION_DELIMITER = ".";
+    private static final String VERSION_PREFIX = "v";
+
+    // split by , . or -
+    private static final Pattern PARSER_PATTERN = Pattern.compile( "\\.|-|," );
+
+    public static final VersionNumber ZERO = VersionNumber.of( 0, 0, 0 );
+
+    private static final Comparator<VersionNumber> COMPARATOR = Comparator
+            .comparingInt( VersionNumber::getMajor )
+            .thenComparingInt( VersionNumber::getMinor )
+            .thenComparingInt( VersionNumber::getPatch );
+
+    private final int major;
+    private final int minor;
+    private final int patch;
+
+    @Override
+    public int compareTo( final VersionNumber o )
+    {
+        return COMPARATOR.compare( this, o );
+    }
+
+    public String toString()
+    {
+        return prettyVersionString();
+    }
+
+    public String prettyVersionString()
+    {
+        return VERSION_PREFIX + major + VERSION_DELIMITER + minor + VERSION_DELIMITER + patch;
+    }
+
+    public static VersionNumber parse( final String input )
+    {
+        Objects.requireNonNull( input );
+
+        final String prefixStripped = input.toLowerCase().startsWith( VERSION_PREFIX.toLowerCase() )
+                ? input.substring( VERSION_PREFIX.length() )
+                : input;
+
+        final String[] split = PARSER_PATTERN.split( prefixStripped );
+
+        final int major = Integer.parseInt( split[0] );
+        final int minor = split.length > 1 ? Integer.parseInt( split[1] ) : 0;
+        final int patch = split.length > 2 ? Integer.parseInt( split[2] ) : 0;
+
+        return new VersionNumber( major, minor, patch );
+    }
+
+    public static VersionNumber of( final int major, final int minor, final int patch )
+    {
+        return new VersionNumber( major, minor, patch );
+    }
+}

+ 37 - 0
lib-data/src/main/java/password/pwm/bean/pub/PublishVersionBean.java

@@ -0,0 +1,37 @@
+/*
+ * Password Management Servlets (PWM)
+ * http://www.pwm-project.org
+ *
+ * Copyright (c) 2006-2009 Novell, Inc.
+ * Copyright (c) 2009-2021 The PWM Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package password.pwm.bean.pub;
+
+import lombok.Value;
+import password.pwm.bean.VersionNumber;
+
+import java.util.Map;
+
+@Value
+public class PublishVersionBean
+{
+    private final Map<VersionKey, VersionNumber> versions;
+
+    public enum VersionKey
+    {
+        current,
+    }
+}

+ 67 - 0
lib-data/src/test/java/password/pwm/bean/VersionNumberTest.java

@@ -0,0 +1,67 @@
+/*
+ * Password Management Servlets (PWM)
+ * http://www.pwm-project.org
+ *
+ * Copyright (c) 2006-2009 Novell, Inc.
+ * Copyright (c) 2009-2021 The PWM Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package password.pwm.bean;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class VersionNumberTest
+{
+    @Test
+    public void testParse()
+    {
+        {
+            final VersionNumber versionNumber = VersionNumber.parse( "v1.2.3" );
+            Assert.assertEquals( VersionNumber.of( 1, 2, 3 ), versionNumber );
+        }
+
+        {
+            final VersionNumber versionNumber = VersionNumber.parse( "v1.2" );
+            Assert.assertEquals( VersionNumber.of( 1, 2, 0 ), versionNumber );
+        }
+    }
+
+    @Test
+    public void testComparator()
+    {
+        final List<VersionNumber> list = new ArrayList<>();
+
+        list.add( VersionNumber.of( 3, 2, 3  ) );
+        list.add( VersionNumber.of( 1, 4, 5  ) );
+        list.add( VersionNumber.of( 1, 3, 5  ) );
+        list.add( VersionNumber.of( 1, 3, 3  ) );
+        list.add( VersionNumber.of( 42, 2, 1  ) );
+        list.add( VersionNumber.of( 7, 0, 11  ) );
+
+        Collections.sort( list );
+
+        Assert.assertEquals( VersionNumber.of( 1, 3, 3 ), list.get( 0 ) );
+        Assert.assertEquals( VersionNumber.of( 1, 3, 5 ), list.get( 1 ) );
+        Assert.assertEquals( VersionNumber.of( 1, 4, 5 ), list.get( 2 ) );
+        Assert.assertEquals( VersionNumber.of( 3, 2, 3 ), list.get( 3 ) );
+        Assert.assertEquals( VersionNumber.of( 7, 0, 11 ), list.get( 4 ) );
+        Assert.assertEquals( VersionNumber.of( 42, 2, 1 ), list.get( 5 ) );
+    }
+}