galovics commented on code in PR #2308:
URL: https://github.com/apache/fineract/pull/2308#discussion_r864700999


##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java:
##########
@@ -60,5 +60,25 @@ public static class FineractModeProperties {
         public boolean isReadOnlyMode() {
             return readEnabled && !writeEnabled && !batchEnabled;
         }
+
+        public boolean isWriteOnlyMode() {
+            return !readEnabled && writeEnabled && !batchEnabled;
+        }
+
+        public boolean isBatchOnlyMode() {

Review Comment:
   Why do we need this?



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java:
##########
@@ -60,5 +60,25 @@ public static class FineractModeProperties {
         public boolean isReadOnlyMode() {
             return readEnabled && !writeEnabled && !batchEnabled;
         }
+
+        public boolean isWriteOnlyMode() {
+            return !readEnabled && writeEnabled && !batchEnabled;
+        }
+
+        public boolean isBatchOnlyMode() {
+            return !readEnabled && !writeEnabled && batchEnabled;
+        }
+
+        public boolean isReadInstance() {

Review Comment:
   What about `isReadEnabled()`? It's already available on the class.



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java:
##########
@@ -60,5 +60,25 @@ public static class FineractModeProperties {
         public boolean isReadOnlyMode() {
             return readEnabled && !writeEnabled && !batchEnabled;
         }
+
+        public boolean isWriteOnlyMode() {

Review Comment:
   Why do we need this?



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java:
##########
@@ -60,5 +60,25 @@ public static class FineractModeProperties {
         public boolean isReadOnlyMode() {
             return readEnabled && !writeEnabled && !batchEnabled;
         }
+
+        public boolean isWriteOnlyMode() {
+            return !readEnabled && writeEnabled && !batchEnabled;
+        }
+
+        public boolean isBatchOnlyMode() {
+            return !readEnabled && !writeEnabled && batchEnabled;
+        }
+
+        public boolean isReadInstance() {
+            return isReadOnlyMode() || readEnabled;
+        }
+
+        public boolean isWriteInstance() {
+            return isWriteOnlyMode() || writeEnabled;
+        }
+
+        public boolean isBatchInstance() {

Review Comment:
   `isBatchEnabled()`?



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/config/FineractProperties.java:
##########
@@ -60,5 +60,25 @@ public static class FineractModeProperties {
         public boolean isReadOnlyMode() {
             return readEnabled && !writeEnabled && !batchEnabled;
         }
+
+        public boolean isWriteOnlyMode() {
+            return !readEnabled && writeEnabled && !batchEnabled;
+        }
+
+        public boolean isBatchOnlyMode() {
+            return !readEnabled && !writeEnabled && batchEnabled;
+        }
+
+        public boolean isReadInstance() {
+            return isReadOnlyMode() || readEnabled;
+        }
+
+        public boolean isWriteInstance() {

Review Comment:
   `isWriteEnabled()`?



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/data/ApiGlobalErrorResponse.java:
##########
@@ -72,7 +78,17 @@ public static ApiGlobalErrorResponse 
invalidTenantIdentifier() {
         globalErrorResponse.setHttpStatusCode("401");
         globalErrorResponse.setDeveloperMessage("Invalid tenant details were 
passed in api request.");
         
globalErrorResponse.setUserMessageGlobalisationCode("error.msg.invalid.tenant.identifier");
-        globalErrorResponse.setDefaultUserMessage("Invalide tenant identifier 
provided with request.");
+        globalErrorResponse.setDefaultUserMessage("Invalid tenant identifier 
provided with request.");
+
+        return globalErrorResponse;
+    }
+
+    public static ApiGlobalErrorResponse invalidInstanceTypeMethod(final 
String method) {
+        final ApiGlobalErrorResponse globalErrorResponse = new 
ApiGlobalErrorResponse();
+        globalErrorResponse.setHttpStatusCode("405");

Review Comment:
   HttpStatus enum pls.



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/exceptionmapper/InvalidTenantIdentifierExceptionMapper.java:
##########
@@ -42,7 +42,7 @@ public class InvalidTenantIdentifierExceptionMapper 
implements ExceptionMapper<I
 
     @Override
     public Response toResponse(@SuppressWarnings("unused") final 
InvalidTenantIdentiferException e) {
-        return 
Response.status(Status.UNAUTHORIZED).entity(ApiGlobalErrorResponse.invalidTenantIdentifier())
+        return 
Response.status(Status.METHOD_NOT_ALLOWED).entity(ApiGlobalErrorResponse.invalidTenantIdentifier())

Review Comment:
   Why the change?



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/security/filter/FineractInstanceModeApiFilter.java:
##########
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fineract.infrastructure.security.filter;
+
+import java.io.IOException;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.HttpMethod;
+import javax.ws.rs.ext.Provider;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.time.StopWatch;
+import org.apache.fineract.infrastructure.core.config.FineractProperties;
+import org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse;
+import 
org.apache.fineract.infrastructure.core.serialization.ToApiJsonSerializer;
+import org.apache.fineract.infrastructure.security.data.PlatformRequestLog;
+import org.apache.http.HttpStatus;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.GenericFilterBean;
+
+@Provider
+@Component
+@Slf4j
+public class FineractInstanceModeApiFilter extends GenericFilterBean {
+
+    @Autowired
+    private FineractProperties fineractProperties;
+
+    @Autowired
+    private ToApiJsonSerializer<PlatformRequestLog> toApiJsonSerializer;
+
+    @Override
+    public void doFilter(final ServletRequest req, final ServletResponse res, 
final FilterChain chain)
+            throws IOException, ServletException {
+
+        final HttpServletRequest request = (HttpServletRequest) req;
+        final HttpServletResponse response = (HttpServletResponse) res;
+
+        log.debug("{} {} {} {}", request.getMethod(), 
fineractProperties.getMode().isReadEnabled(),
+                fineractProperties.getMode().isWriteEnabled(), 
fineractProperties.getMode().isBatchEnabled());
+
+        final StopWatch task = new StopWatch();
+        task.start();
+
+        if (fineractProperties.getMode().isReadInstance() && 
isReadMethod(request)) {
+            chain.doFilter(request, response);
+        } else if (fineractProperties.getMode().isWriteInstance() && 
isWriteMethod(request)) {
+            chain.doFilter(request, response);
+        } else {
+            task.stop();
+            final PlatformRequestLog platformRequestLog = 
PlatformRequestLog.from(task, request);

Review Comment:
   Why do we need this `PlatformRequestLog`?



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/security/filter/FineractInstanceModeApiFilter.java:
##########
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fineract.infrastructure.security.filter;
+
+import java.io.IOException;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.HttpMethod;
+import javax.ws.rs.ext.Provider;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.time.StopWatch;
+import org.apache.fineract.infrastructure.core.config.FineractProperties;
+import org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse;
+import 
org.apache.fineract.infrastructure.core.serialization.ToApiJsonSerializer;
+import org.apache.fineract.infrastructure.security.data.PlatformRequestLog;
+import org.apache.http.HttpStatus;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.GenericFilterBean;
+
+@Provider
+@Component
+@Slf4j
+public class FineractInstanceModeApiFilter extends GenericFilterBean {
+
+    @Autowired
+    private FineractProperties fineractProperties;
+
+    @Autowired
+    private ToApiJsonSerializer<PlatformRequestLog> toApiJsonSerializer;
+
+    @Override
+    public void doFilter(final ServletRequest req, final ServletResponse res, 
final FilterChain chain)
+            throws IOException, ServletException {
+
+        final HttpServletRequest request = (HttpServletRequest) req;
+        final HttpServletResponse response = (HttpServletResponse) res;
+
+        log.debug("{} {} {} {}", request.getMethod(), 
fineractProperties.getMode().isReadEnabled(),
+                fineractProperties.getMode().isWriteEnabled(), 
fineractProperties.getMode().isBatchEnabled());
+
+        final StopWatch task = new StopWatch();

Review Comment:
   Why do we need this?



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/security/filter/FineractInstanceModeApiFilter.java:
##########
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fineract.infrastructure.security.filter;
+
+import java.io.IOException;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.HttpMethod;
+import javax.ws.rs.ext.Provider;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.time.StopWatch;
+import org.apache.fineract.infrastructure.core.config.FineractProperties;
+import org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse;
+import 
org.apache.fineract.infrastructure.core.serialization.ToApiJsonSerializer;
+import org.apache.fineract.infrastructure.security.data.PlatformRequestLog;
+import org.apache.http.HttpStatus;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.GenericFilterBean;
+
+@Provider
+@Component
+@Slf4j
+public class FineractInstanceModeApiFilter extends GenericFilterBean {
+
+    @Autowired
+    private FineractProperties fineractProperties;
+
+    @Autowired
+    private ToApiJsonSerializer<PlatformRequestLog> toApiJsonSerializer;
+
+    @Override
+    public void doFilter(final ServletRequest req, final ServletResponse res, 
final FilterChain chain)
+            throws IOException, ServletException {
+
+        final HttpServletRequest request = (HttpServletRequest) req;
+        final HttpServletResponse response = (HttpServletResponse) res;
+
+        log.debug("{} {} {} {}", request.getMethod(), 
fineractProperties.getMode().isReadEnabled(),
+                fineractProperties.getMode().isWriteEnabled(), 
fineractProperties.getMode().isBatchEnabled());
+
+        final StopWatch task = new StopWatch();
+        task.start();
+
+        if (fineractProperties.getMode().isReadInstance() && 
isReadMethod(request)) {
+            chain.doFilter(request, response);
+        } else if (fineractProperties.getMode().isWriteInstance() && 
isWriteMethod(request)) {
+            chain.doFilter(request, response);
+        } else {
+            task.stop();
+            final PlatformRequestLog platformRequestLog = 
PlatformRequestLog.from(task, request);
+            log.debug("{}", 
this.toApiJsonSerializer.serialize(platformRequestLog));

Review Comment:
   This is definitely something we don't want. I mean why do we want to turn an 
object into a JSON within the logs? It's gonna be unreadable anyway.



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/security/filter/FineractInstanceModeApiFilter.java:
##########
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fineract.infrastructure.security.filter;
+
+import java.io.IOException;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.HttpMethod;
+import javax.ws.rs.ext.Provider;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.time.StopWatch;
+import org.apache.fineract.infrastructure.core.config.FineractProperties;
+import org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse;
+import 
org.apache.fineract.infrastructure.core.serialization.ToApiJsonSerializer;
+import org.apache.fineract.infrastructure.security.data.PlatformRequestLog;
+import org.apache.http.HttpStatus;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.GenericFilterBean;
+
+@Provider
+@Component
+@Slf4j
+public class FineractInstanceModeApiFilter extends GenericFilterBean {

Review Comment:
   `OncePerRequestFilter`?



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/security/filter/FineractInstanceModeApiFilter.java:
##########
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fineract.infrastructure.security.filter;
+
+import java.io.IOException;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.HttpMethod;
+import javax.ws.rs.ext.Provider;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.time.StopWatch;
+import org.apache.fineract.infrastructure.core.config.FineractProperties;
+import org.apache.fineract.infrastructure.core.data.ApiGlobalErrorResponse;
+import 
org.apache.fineract.infrastructure.core.serialization.ToApiJsonSerializer;
+import org.apache.fineract.infrastructure.security.data.PlatformRequestLog;
+import org.apache.http.HttpStatus;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.GenericFilterBean;
+
+@Provider
+@Component
+@Slf4j
+public class FineractInstanceModeApiFilter extends GenericFilterBean {
+
+    @Autowired
+    private FineractProperties fineractProperties;
+
+    @Autowired
+    private ToApiJsonSerializer<PlatformRequestLog> toApiJsonSerializer;
+
+    @Override
+    public void doFilter(final ServletRequest req, final ServletResponse res, 
final FilterChain chain)
+            throws IOException, ServletException {
+
+        final HttpServletRequest request = (HttpServletRequest) req;
+        final HttpServletResponse response = (HttpServletResponse) res;
+
+        log.debug("{} {} {} {}", request.getMethod(), 
fineractProperties.getMode().isReadEnabled(),
+                fineractProperties.getMode().isWriteEnabled(), 
fineractProperties.getMode().isBatchEnabled());
+
+        final StopWatch task = new StopWatch();
+        task.start();
+
+        if (fineractProperties.getMode().isReadInstance() && 
isReadMethod(request)) {
+            chain.doFilter(request, response);
+        } else if (fineractProperties.getMode().isWriteInstance() && 
isWriteMethod(request)) {
+            chain.doFilter(request, response);
+        } else {
+            task.stop();
+            final PlatformRequestLog platformRequestLog = 
PlatformRequestLog.from(task, request);
+            log.debug("{}", 
this.toApiJsonSerializer.serialize(platformRequestLog));
+            response.setStatus(HttpStatus.SC_METHOD_NOT_ALLOWED);
+            ApiGlobalErrorResponse errorResponse = 
ApiGlobalErrorResponse.invalidInstanceTypeMethod(request.getMethod());
+            response.getWriter().write(errorResponse.toJson());
+        }
+    }
+
+    private boolean isReadMethod(HttpServletRequest request) {
+        return HttpMethod.GET.equals(request.getMethod());
+    }
+
+    private boolean isWriteMethod(HttpServletRequest request) {
+        return !HttpMethod.GET.equals(request.getMethod());

Review Comment:
   Maybe negating the isReadMethod is more readable, like `!isReadMethod()`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to