-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from anandvarkeyphilips/development
Development branch merged
- Loading branch information
Showing
169 changed files
with
20,954 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
package io.exnihilo.validator.controller; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import io.exnihilo.validator.service.ValidatorService; | ||
import io.swagger.annotations.ApiOperation; | ||
import io.swagger.annotations.ApiResponse; | ||
import io.swagger.annotations.ApiResponses; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.yaml.snakeyaml.Yaml; | ||
import org.springframework.web.servlet.ModelAndView; | ||
import springfox.documentation.builders.PathSelectors; | ||
import springfox.documentation.builders.RequestHandlerSelectors; | ||
import springfox.documentation.service.ApiInfo; | ||
|
@@ -13,11 +24,13 @@ | |
import springfox.documentation.spring.web.plugins.Docket; | ||
|
||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@RestController | ||
public class ValidatorController { | ||
|
||
@Autowired | ||
private ValidatorService validatorService; | ||
|
||
@Bean | ||
public Docket api() { | ||
|
@@ -28,30 +41,42 @@ public Docket api() { | |
.build().apiInfo(apiInfo()); | ||
} | ||
|
||
|
||
private ApiInfo apiInfo() { | ||
return new ApiInfo( | ||
"This REST API", | ||
"Validates yaml, json and xml files. Hi!!", | ||
"API TOS", | ||
"Terms of service", | ||
new Contact("Anand Varkey Philips", "about.me/anandvarkeyphilips", "[email protected]"), | ||
"License of API", "API license URL", Collections.emptyList()); | ||
} | ||
"This REST API", | ||
"Validates yaml, json and xml files. Hi!!", | ||
"API TOS", | ||
"Terms of service", | ||
new Contact("Anand Varkey Philips", "about.me/anandvarkeyphilips", "[email protected]"), | ||
"License of API", "API license URL", Collections.emptyList()); | ||
} | ||
|
||
|
||
@RequestMapping("/forEditor") | ||
public ModelAndView forEditor() { | ||
log.info("Inside forEditor"); | ||
return new ModelAndView("yaml-editor"); | ||
} | ||
|
||
/** | ||
* A pre-configured sample REST endpoint to demonstrate the use of Request Parameter. | ||
* | ||
* @param yamlData | ||
* @return validation result | ||
*/ | ||
@PostMapping("/yaml") | ||
public String validateYAML(@RequestBody String yamlData) { | ||
|
||
Yaml yaml = new Yaml(); | ||
try { | ||
Map<String, Object> obj = yaml.load(yamlData); | ||
System.out.println(obj); | ||
return "Validated"; | ||
} catch (ClassCastException e) { | ||
return "Invalid YAML!"; | ||
} catch (Exception e) { | ||
return "Error occured in validation"; | ||
} | ||
@ApiOperation( | ||
value = "API for Validating the YAML Data", | ||
notes = "This API validates YAML data input.The API is in beta phase..") | ||
@ApiResponses(value = { | ||
@ApiResponse(code = 200, message = "Successfully connected and validated with API Validator"), | ||
@ApiResponse(code = 401, message = "You are not authenticated properly to view the resource!"), | ||
@ApiResponse(code = 403, message = "Accessing the resource you were trying to reach is forbidden!"), | ||
@ApiResponse(code = 404, message = "Validator Service not available right now!"), | ||
}) | ||
public ResponseEntity<?> validateYAMLController(@RequestBody ObjectNode yamlData) { | ||
log.debug("Calling validateYAMLController..."); | ||
return new ResponseEntity<Object>(validatorService.validateYAMLService(yamlData.get("yamlData").asText()), HttpStatus.OK); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/io/exnihilo/validator/entity/ValidationResponseEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.exnihilo.validator.entity; | ||
|
||
import lombok.Data; | ||
import org.springframework.context.annotation.Scope; | ||
import org.springframework.context.annotation.ScopedProxyMode; | ||
import org.springframework.stereotype.Component; | ||
/** | ||
* Validation Response Entity has the response details for all configured validator methods. | ||
* | ||
* @author Anand Varkey Philips | ||
* @date 27/10/2018 | ||
* @since 2.0.0.RELEASE | ||
*/ | ||
@Component | ||
@Data | ||
@Scope(value = "prototype", proxyMode = ScopedProxyMode.INTERFACES) | ||
public class ValidationResponseEntity { | ||
private int lineNumber; | ||
private int columnNumber; | ||
private String validationMessage; | ||
} |
66 changes: 65 additions & 1 deletion
66
src/main/java/io/exnihilo/validator/service/ValidatorService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,71 @@ | ||
package io.exnihilo.validator.service; | ||
|
||
import io.exnihilo.validator.entity.ValidationResponseEntity; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Lookup; | ||
import org.springframework.stereotype.Service; | ||
import org.yaml.snakeyaml.Yaml; | ||
|
||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Validator Service Class handles the functional aspects of all configured validators. | ||
* | ||
* @author Anand Varkey Philips | ||
* @date 27/10/2018 | ||
* @since 2.0.0.RELEASE | ||
*/ | ||
@Slf4j | ||
@Service | ||
public class ValidatorService { | ||
} | ||
|
||
@Lookup | ||
public ValidationResponseEntity getPrototypeBean() { | ||
return null; | ||
} | ||
|
||
|
||
/** | ||
* Splitting of yaml data in case of "---", then validating each part separately, | ||
* and then returning success data or line and column numbers in case of failure. | ||
* | ||
* @param yamlData | ||
* @return validation result | ||
*/ | ||
public ValidationResponseEntity validateYAMLService(String yamlData) { | ||
ValidationResponseEntity validationResponseEntity = getPrototypeBean(); | ||
String validationMessage = "Valid YAML!!!"; | ||
try { | ||
Yaml yaml = new Yaml(); | ||
Map<String, Object> obj = yaml.load(yamlData.replace("---", "")); | ||
log.debug("Value obtained successfully: {}", obj.toString()); | ||
validationResponseEntity.setValidationMessage(validationMessage); | ||
} catch (Exception e) { | ||
validationMessage = e.getMessage(); | ||
validationResponseEntity.setValidationMessage(validationMessage); | ||
log.error("Exception occurred in validation: ", e); | ||
if (validationMessage.contains("line ")) { | ||
String pattern1 = "line "; | ||
String pattern2 = ","; | ||
Pattern p = Pattern.compile(Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2)); | ||
Matcher m = p.matcher(validationMessage); | ||
while (m.find()) { | ||
validationResponseEntity.setLineNumber(Integer.parseInt(m.group(1))); | ||
} | ||
} | ||
if (validationMessage.contains("line ")) { | ||
String pattern1 = "column "; | ||
String pattern2 = ":"; | ||
Pattern p = Pattern.compile(Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2)); | ||
Matcher m = p.matcher(validationMessage); | ||
while (m.find()) { | ||
validationResponseEntity.setColumnNumber(Integer.parseInt(m.group(1))); | ||
} | ||
} | ||
} finally { | ||
return validationResponseEntity; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.