Skip to content

Commit

Permalink
Merge pull request #1 from anandvarkeyphilips/development
Browse files Browse the repository at this point in the history
Development branch merged
  • Loading branch information
anandvarkeyphilips authored Oct 27, 2018
2 parents 2673fdd + 7072523 commit 450de68
Show file tree
Hide file tree
Showing 169 changed files with 20,954 additions and 27 deletions.
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.exnihilo</groupId>
<artifactId>yaml-validator</artifactId>
<version>1.0.5.RELEASE</version>
<version>2.0.0.RELEASE</version>
<packaging>jar</packaging>

<properties>
Expand All @@ -24,6 +24,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand All @@ -44,6 +48,10 @@
<artifactId>snakeyaml</artifactId>
<version>1.23</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
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;
Expand All @@ -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() {
Expand All @@ -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);
}
}
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;
}
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;
}
}
}
3 changes: 0 additions & 3 deletions src/main/resources/customer.yaml

This file was deleted.

6 changes: 6 additions & 0 deletions src/main/resources/static/css/bootstrap-theme.min.css

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/main/resources/static/css/bootstrap.min.css

Large diffs are not rendered by default.

Loading

0 comments on commit 450de68

Please sign in to comment.