Skip to content

Commit

Permalink
swagger config
Browse files Browse the repository at this point in the history
  • Loading branch information
indraniBan authored and indraniBan committed Aug 11, 2024
1 parent 32ed481 commit 0d040e0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 177 deletions.
24 changes: 7 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.17.0-rc1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.17.0-rc1</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
Expand All @@ -153,6 +158,7 @@
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>2.17.0</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -235,23 +241,7 @@
<finalName>fhirapi-v1.0</finalName>

<plugins>
<plugin>
<groupId>org.owasp</groupId>
<artifactId>dependency-check-maven</artifactId>
<version>9.0.8</version>
<configuration>
<format>HTML</format>
<nvdApiServerId>nvd</nvdApiServerId>
<nvdApiKey>79597933-f3cc-4601-949b-12513434ade3</nvdApiKey>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand Down
72 changes: 24 additions & 48 deletions src/main/java/com/wipro/fhir/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,24 @@
/*
* AMRIT – Accessible Medical Records via Integrated Technology Integrated EHR
* (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see https://www.gnu.org/licenses/.
*
* package com.wipro.fhir.config;
*
* import org.springframework.context.annotation.Bean; import
* org.springframework.context.annotation.Configuration;
*
* import springfox.documentation.builders.RequestHandlerSelectors; import
* springfox.documentation.service.ApiInfo; import
* springfox.documentation.service.Contact; import
* springfox.documentation.spi.DocumentationType; import
* springfox.documentation.spring.web.plugins.Docket; import
* springfox.documentation.swagger2.annotations.EnableSwagger2;
*
* @Configuration
*
* @EnableSwagger2 public class SwaggerConfig {
*
* @Bean public Docket productApi() { return new
* Docket(DocumentationType.SWAGGER_2).select()
* .apis(RequestHandlerSelectors.basePackage("com.wipro.fhir.controller")).build
* ().apiInfo(metaData()); }
*
* private ApiInfo metaData() { ApiInfo apiInfo = new ApiInfo("FHIR API",
* "Service that implements FHIR standard for healthcare information exchange between various systems that are used by clinicians and organisations."
* , "1.0", "Terms of service", new Contact("AMRIT",
* "https://psmri.github.io/PSMRI/", "[email protected]"), "", "");
* return apiInfo; } }
*/
package com.wipro.fhir.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;

@Configuration
public class SwaggerConfig {

@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI().info(new
Info().title("FHIR API").version("version").description("A microservice for the creation and management of beneficiaries."))
.addSecurityItem(new SecurityRequirement().addList("my security"))
.components(new Components().addSecuritySchemes("my security",
new SecurityScheme().name("my security").type(SecurityScheme.Type.HTTP).scheme("bearer")));
}

}
74 changes: 43 additions & 31 deletions src/main/java/com/wipro/fhir/utils/http/HTTPRequestInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,66 +38,71 @@

@Component
public class HTTPRequestInterceptor implements HandlerInterceptor {
private Validator validator;

Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
Logger logger = LoggerFactory.getLogger(this.getClass().getName());

@Autowired
public void setValidator(Validator validator) {
this.validator = validator;
}

private SessionObject sessionObject;

@Autowired
public void setSessionObject(SessionObject sessionObject) {
this.sessionObject = sessionObject;
}

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
logger.info("http interceptor - pre Handle");
boolean status = true;
logger.debug("In preHandle we are Intercepting the Request");
String authorization = request.getHeader("Authorization");
logger.debug("RequestURI::" + request.getRequestURI() + " || Authorization ::" + authorization
+ " || method :: " + request.getMethod());

if (request.getRequestURI().toLowerCase().contains("swagger-ui"))
return status;

String authorization = null;
String preAuth = request.getHeader("Authorization");
if (null != preAuth && preAuth.contains("Bearer "))
authorization = preAuth.replace("Bearer ", "");
else
authorization = preAuth;
if (!request.getMethod().equalsIgnoreCase("OPTIONS")) {
try {
String[] requestURIParts = request.getRequestURI().split("/");
String requestAPI = requestURIParts[requestURIParts.length - 1];
switch (requestAPI) {

// case "patient":
case "swagger-ui.html":
break;
case "index.html":
break;
case "swagger-initializer.js":
break;
case "swagger-config":
break;
case "ui":
break;
case "swagger-resources":
case "version":
break;
case "api-docs":

break;

case "error":
status = false;
break;
default:
String remoteAddress = request.getHeader("X-FORWARDED-FOR");
if (remoteAddress == null || remoteAddress.trim().length() == 0) {
remoteAddress = request.getRemoteAddr();
}
validator.checkKeyExists(authorization, remoteAddress);
logger.debug("RequestURI::" + request.getRequestURI() + " || Authorization ::" + authorization);
if (authorization == null)
throw new Exception(
"Authorization key is NULL, please pass valid session key to proceed further. ");
String userRespFromRedis = sessionObject.getSessionObject(authorization);
if (userRespFromRedis == null)
throw new Exception("invalid Authorization key, please pass a valid key to proceed further. ");
break;
}
} catch (Exception e) {
logger.error(e.getLocalizedMessage());

OutputResponse output = new OutputResponse();
output.setError(e);
response.getOutputStream().print(output.toString());
response.setContentType(MediaType.APPLICATION_JSON);

response.setContentLength(output.toString().length());
response.setHeader("Access-Control-Allow-Origin", "*");
response.getOutputStream().print(output.toString());

status = false;
}
}

return status;
}

Expand All @@ -106,7 +111,12 @@ public void postHandle(HttpServletRequest request, HttpServletResponse response,
throws Exception {
try {
logger.debug("In postHandle we are Intercepting the Request");
String authorization = request.getHeader("Authorization");
String authorization = null;
String postAuth = request.getHeader("Authorization");
if (null != postAuth && postAuth.contains("Bearer "))
authorization = postAuth.replace("Bearer ", "");
else
authorization = postAuth;
logger.debug("RequestURI::" + request.getRequestURI() + " || Authorization ::" + authorization);
if (authorization != null) {
sessionObject.updateSessionObject(authorization, sessionObject.getSessionObject(authorization));
Expand All @@ -119,6 +129,8 @@ public void postHandle(HttpServletRequest request, HttpServletResponse response,
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception arg3)
throws Exception {
logger.debug("In afterCompletion Request Completed");
logger.info("http interceptor - after completion");

}
}

}

This file was deleted.

35 changes: 0 additions & 35 deletions src/test/java/com/wipro/fhir/config/InterceptorConfigTest.java

This file was deleted.

0 comments on commit 0d040e0

Please sign in to comment.