[EDIT] SE COMENTO LINEAS DE LOS SERVICIOS PARA EVITAR ERRORES

parent a7e8689d
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web version="8.0" xmlns="http://www.jboss.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/schema/jbossas/jboss-web_8_0.xsd">
<!-- <context-root>/</context-root>-->
</jboss-web>
\ No newline at end of file
package web.multitask.trismegistoservices.api; //package web.multitask.trismegistoservices.api;
//
import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; //import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; //import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; //import org.springframework.web.bind.annotation.*;
//
import web.multitask.trismegistoservices.model.DriveRequest; //import web.multitask.trismegistoservices.model.DriveRequest;
import web.multitask.trismegistoservices.service.DriveService; //import web.multitask.trismegistoservices.service.DriveService;
//
@RestController //@RestController
@CrossOrigin(origins = "*") //@CrossOrigin(origins = "*")
@RequestMapping("drive") //@RequestMapping("drive")
public class DriveApi { //public class DriveApi {
//
//
@Autowired // @Autowired
private DriveService driveService; // private DriveService driveService;
//
@PostMapping(path = "/public/upload", consumes = { // @PostMapping(path = "/public/upload", consumes = {
MediaType.MULTIPART_FORM_DATA_VALUE }) // MediaType.MULTIPART_FORM_DATA_VALUE })
public ResponseEntity<?> uploadFile(@ModelAttribute DriveRequest request) { // public ResponseEntity<?> uploadFile(@ModelAttribute DriveRequest request) {
//
String folder_id = request.getFolder_id(); // String folder_id = request.getFolder_id();
String file_name = request.getFile_name(); // String file_name = request.getFile_name();
String base64_name = request.getBase64_name(); // String base64_name = request.getBase64_name();
String base64_file = request.getBase64_file(); // String base64_file = request.getBase64_file();
//
System.out.println(request.getFile().getOriginalFilename()); // System.out.println(request.getFile().getOriginalFilename());
//
return null; // return null;
} // }
//
} //}
\ No newline at end of file \ No newline at end of file
...@@ -10,7 +10,7 @@ import web.multitask.trismegistoservices.service.PDFService; ...@@ -10,7 +10,7 @@ import web.multitask.trismegistoservices.service.PDFService;
@RestController @RestController
@CrossOrigin(origins = "*") @CrossOrigin(origins = "*")
@RequestMapping("pdf") @RequestMapping("/pdf")
public class PDFApi { public class PDFApi {
PDFService pdfService; PDFService pdfService;
......
package web.multitask.trismegistoservices.api; // package web.multitask.trismegistoservices.api;
import org.json.JSONArray; // import org.json.JSONArray;
import org.json.JSONObject; // import org.json.JSONObject;
import org.springframework.core.io.FileSystemResource; // import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource; // import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders; // import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; // import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; // import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; // import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; // import org.springframework.web.multipart.MultipartFile;
import web.multitask.trismegistoservices.model.FileRequest; // import web.multitask.trismegistoservices.model.FileRequest;
import web.multitask.trismegistoservices.utils.CommonUtils; // import web.multitask.trismegistoservices.utils.CommonUtils;
import java.io.File; // import java.io.File;
import java.util.Arrays; // import java.util.Arrays;
@RestController // @RestController
@CrossOrigin(origins = "*") // @CrossOrigin(origins = "*")
@RequestMapping("utils") // @RequestMapping("utils")
public class UtilsApi { // public class UtilsApi {
CommonUtils commonUtils; // CommonUtils commonUtils;
public UtilsApi(CommonUtils commonUtils) { // public UtilsApi(CommonUtils commonUtils) {
this.commonUtils = commonUtils; // this.commonUtils = commonUtils;
} // }
@RequestMapping(path = "/public/file/base64", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) // @RequestMapping(path = "/public/file/base64", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<?> fileBase64(@ModelAttribute FileRequest request) { // public ResponseEntity<?> fileBase64(@ModelAttribute FileRequest request) {
JSONObject response = new JSONObject(); // JSONObject response = new JSONObject();
try{ // try{
MultipartFile[] files = request.getFiles(); // MultipartFile[] files = request.getFiles();
JSONArray jsonArray = new JSONArray(); // JSONArray jsonArray = new JSONArray();
Arrays.stream(files).forEach(file -> { // Arrays.stream(files).forEach(file -> {
jsonArray.put(commonUtils.convertToBase64(file)); // jsonArray.put(commonUtils.convertToBase64(file));
}); // });
response.put("files", jsonArray); // response.put("files", jsonArray);
response.put("message", "Success"); // response.put("message", "Success");
response.put("status", true); // response.put("status", true);
return ResponseEntity.ok(response.toMap()); // return ResponseEntity.ok(response.toMap());
}catch (Exception e){ // }catch (Exception e){
System.out.println(e); // System.out.println(e);
return ResponseEntity.badRequest().body(e.getMessage()); // return ResponseEntity.badRequest().body(e.getMessage());
} // }
} // }
@PostMapping("/public/base64/file") // @PostMapping("/public/base64/file")
public ResponseEntity<Resource> base64File(@RequestBody String json) { // public ResponseEntity<Resource> base64File(@RequestBody String json) {
try{ // try{
JSONObject bodyJson = new JSONObject(json); // JSONObject bodyJson = new JSONObject(json);
HttpHeaders headers = new HttpHeaders(); // HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + bodyJson.getString("name")); // headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + bodyJson.getString("name"));
File outputFile = commonUtils.convertToFile(bodyJson.getString("base64"),bodyJson.getString("name")); // File outputFile = commonUtils.convertToFile(bodyJson.getString("base64"),bodyJson.getString("name"));
if (outputFile != null) { // if (outputFile != null) {
Resource resource = new FileSystemResource(outputFile); // Resource resource = new FileSystemResource(outputFile);
return ResponseEntity.ok() // return ResponseEntity.ok()
.headers(headers) // .headers(headers)
.body(resource); // .body(resource);
} else { // } else {
return ResponseEntity.badRequest().body(null); // return ResponseEntity.badRequest().body(null);
} // }
}catch (Exception e){ // }catch (Exception e){
System.out.println(e); // System.out.println(e);
return ResponseEntity.badRequest().body(null); // return ResponseEntity.badRequest().body(null);
} // }
} // }
} // }
\ No newline at end of file \ No newline at end of file
package web.multitask.trismegistoservices.config; // package web.multitask.trismegistoservices.config;
import org.springframework.context.annotation.Bean; // import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; // import org.springframework.context.annotation.Configuration;
import io.github.cdimascio.dotenv.Dotenv; // import io.github.cdimascio.dotenv.Dotenv;
import web.multitask.trismegistoservices.utils.DotEnvUtil; // import web.multitask.trismegistoservices.utils.DotEnvUtil;
@Configuration // @Configuration
public class DotEnvConfig { // public class DotEnvConfig {
@Bean // @Bean
Dotenv getDotEnvPath() { // Dotenv getDotEnvPath() {
return Dotenv.configure().directory(DotEnvUtil.getDotEnvPath("fullservice")).load(); // return Dotenv.configure().directory(DotEnvUtil.getDotEnvPath("trismegisto-asistencia")).load();
} // }
} // }
\ No newline at end of file \ No newline at end of file
...@@ -41,11 +41,8 @@ public class JWTokenFilter extends OncePerRequestFilter { ...@@ -41,11 +41,8 @@ public class JWTokenFilter extends OncePerRequestFilter {
throws ServletException, IOException, java.io.IOException { throws ServletException, IOException, java.io.IOException {
response.setContentType("application/json"); response.setContentType("application/json");
// final String queryAuthorization = request.getParameter(HttpHeaders.AUTHORIZATION);
final String Authorization = request.getHeader(HttpHeaders.AUTHORIZATION); final String Authorization = request.getHeader(HttpHeaders.AUTHORIZATION);
String token; String token;
try { try {
token = Authorization.split(" ")[1]; token = Authorization.split(" ")[1];
} catch (Exception e) { } catch (Exception e) {
...@@ -53,7 +50,6 @@ public class JWTokenFilter extends OncePerRequestFilter { ...@@ -53,7 +50,6 @@ public class JWTokenFilter extends OncePerRequestFilter {
} }
if (token == null || token.isEmpty()) { if (token == null || token.isEmpty()) {
// SecurityContextHolder.getContext().setAuthentication(null);
chain.doFilter(request, response); chain.doFilter(request, response);
} else { } else {
if (jwtTokenUtil.validateToken(token)) { if (jwtTokenUtil.validateToken(token)) {
......
package web.multitask.trismegistoservices.service; //package web.multitask.trismegistoservices.service;
//
import java.io.ByteArrayInputStream; //import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException; //import java.io.FileNotFoundException;
import java.io.IOException; //import java.io.IOException;
import java.io.InputStream; //import java.io.InputStream;
import java.io.InputStreamReader; //import java.io.InputStreamReader;
import java.security.GeneralSecurityException; //import java.security.GeneralSecurityException;
import java.util.Collections; //import java.util.Collections;
import java.util.List; //import java.util.List;
//
import org.json.JSONObject; //import org.json.JSONObject;
import org.springframework.stereotype.Component; //import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; //import org.springframework.web.multipart.MultipartFile;
//
import com.google.api.client.auth.oauth2.Credential; //import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; //import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; //import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; //import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; //import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; //import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.InputStreamContent; //import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport; //import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory; //import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory; //import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory; //import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.drive.Drive; //import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes; //import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File; //import com.google.api.services.drive.model.File;
//
@Component //@Component
public class DriveService { //public class DriveService {
//
private static final String APPLICATION_NAME = "FullService Application"; // private static final String APPLICATION_NAME = "FullService Application";
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); // private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens"; // private static final String TOKENS_DIRECTORY_PATH = "tokens";
private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_FILE); // private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_FILE);
private static final String CREDENTIALS_FILE_PATH = "./credentials.json"; // private static final String CREDENTIALS_FILE_PATH = "./credentials.json";
//
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) // private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT)
throws IOException { // throws IOException {
//
InputStream in = DriveService.class.getResourceAsStream(CREDENTIALS_FILE_PATH); // InputStream in = DriveService.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
if (in == null) { // if (in == null) {
throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH); // throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
} // }
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); // GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( // GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) // HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH))) // .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline") // .setAccessType("offline")
.build(); // .build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build(); // LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); // Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
return credential; // return credential;
} // }
//
public Drive getInstance() throws GeneralSecurityException, IOException { // public Drive getInstance() throws GeneralSecurityException, IOException {
// Build a new authorized API client service. // // Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); // final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT)) // Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME) // .setApplicationName(APPLICATION_NAME)
.build(); // .build();
return service; // return service;
} // }
//
//
public String uploadFile(String folder_id, String file_name, MultipartFile file, JSONObject base64) { // public String uploadFile(String folder_id, String file_name, MultipartFile file, JSONObject base64) {
try { // try {
System.out.println(file.getOriginalFilename()); // System.out.println(file.getOriginalFilename());
//
String folderId = folder_id; // String folderId = folder_id;
if (null != file) { // if (null != file) {
File fileMetadata = new File(); // File fileMetadata = new File();
fileMetadata.setParents(Collections.singletonList(folderId)); // fileMetadata.setParents(Collections.singletonList(folderId));
fileMetadata.setName(file.getOriginalFilename()); // fileMetadata.setName(file.getOriginalFilename());
File uploadFile = getInstance() // File uploadFile = getInstance()
.files() // .files()
.create(fileMetadata, new InputStreamContent( // .create(fileMetadata, new InputStreamContent(
file.getContentType(), // file.getContentType(),
new ByteArrayInputStream(file.getBytes())) // new ByteArrayInputStream(file.getBytes()))
) // )
.setFields("id").execute(); // .setFields("id").execute();
System.out.println(uploadFile); // System.out.println(uploadFile);
return uploadFile.getId(); // return uploadFile.getId();
} // }
} catch (Exception e) { // } catch (Exception e) {
System.out.printf("Error: "+ e); // System.out.printf("Error: "+ e);
} // }
return null; // return null;
} // }
} //}
\ No newline at end of file \ No newline at end of file
...@@ -29,9 +29,9 @@ public class PDFService { ...@@ -29,9 +29,9 @@ public class PDFService {
DefaultFontProvider fontProvider = new DefaultFontProvider(false, false, false); DefaultFontProvider fontProvider = new DefaultFontProvider(false, false, false);
fontProvider.addFont("/fonts/Roboto-Regular.ttf"); fontProvider.addFont("/fonts/Roboto-Regular.ttf");
properties.setFontProvider(fontProvider); properties.setFontProvider(fontProvider);
FileOutputStream outputStream = new FileOutputStream("/var/www/html/pdf.pdf"); FileOutputStream outputStream = new FileOutputStream(System.getProperty("java.io.tmpdir") + "/pdf.pdf");
HtmlConverter.convertToPdf(htmlContent, outputStream, properties); HtmlConverter.convertToPdf(htmlContent, outputStream, properties);
File file = new File("/var/www/html/pdf.pdf"); File file = new File(System.getProperty("java.io.tmpdir") + "/pdf.pdf");
return org.apache.commons.io.FileUtils.readFileToByteArray(file); return org.apache.commons.io.FileUtils.readFileToByteArray(file);
} }
......
package web.multitask.trismegistoservices.utils; // package web.multitask.trismegistoservices.utils;
import io.github.cdimascio.dotenv.Dotenv; // import io.github.cdimascio.dotenv.Dotenv;
import org.json.JSONObject; // import org.json.JSONObject;
import org.apache.commons.io.FileUtils; // import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service; // import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; // import org.springframework.web.multipart.MultipartFile;
import java.io.File; // import java.io.File;
import java.util.Arrays; // import java.util.Arrays;
import java.util.Base64; // import java.util.Base64;
import java.util.Objects; // import java.util.Objects;
@Service // @Service
public class CommonUtils { // public class CommonUtils {
private final Dotenv dotenv; // private final Dotenv dotenv;
public CommonUtils( Dotenv dotenv ) { // public CommonUtils( Dotenv dotenv ) {
this.dotenv = dotenv; // this.dotenv = dotenv;
} // }
public JSONObject convertToBase64(MultipartFile file) { // public JSONObject convertToBase64(MultipartFile file) {
JSONObject response = new JSONObject(); // JSONObject response = new JSONObject();
try{ // try{
byte[] byteArray = file.getBytes(); // byte[] byteArray = file.getBytes();
String base64 = Base64.getEncoder().encodeToString(byteArray); // String base64 = Base64.getEncoder().encodeToString(byteArray);
response.put("base64", base64); // response.put("base64", base64);
response.put("name", file.getOriginalFilename()); // response.put("name", file.getOriginalFilename());
response.put("size", file.getSize()); // response.put("size", file.getSize());
response.put("extension", file.getContentType()); // response.put("extension", file.getContentType());
return response; // return response;
} catch (Exception e) { // } catch (Exception e) {
System.out.println(e.getMessage()); // System.out.println(e.getMessage());
return null; // return null;
} // }
} // }
public File convertToFile(String base64, String name) { // public File convertToFile(String base64, String name) {
try{ // try{
byte[] byteArray = Base64.getDecoder().decode(base64); // byte[] byteArray = Base64.getDecoder().decode(base64);
String folder = dotenv.get("FILE_FOLDER") + "/" + name; // String folder = dotenv.get("FILE_FOLDER") + "/" + name;
File outputFile = new File(folder); // File outputFile = new File(folder);
FileUtils.writeByteArrayToFile(outputFile, byteArray); // FileUtils.writeByteArrayToFile(outputFile, byteArray);
return outputFile; // return outputFile;
} catch (Exception e) { // } catch (Exception e) {
System.out.println(e.getMessage()); // System.out.println(e.getMessage());
return null; // return null;
} // }
} // }
public boolean deleteAllFiles() { // public boolean deleteAllFiles() {
try { // try {
String folder= dotenv.get("FILE_FOLDER"); // String folder= dotenv.get("FILE_FOLDER");
assert folder != null; // assert folder != null;
File file = new File(folder); // File file = new File(folder);
if (file.isDirectory()) { // if (file.isDirectory()) {
Boolean[] results = Arrays.stream(Objects.requireNonNull(file.listFiles())).map(File::delete).toArray(Boolean[]::new); // Boolean[] results = Arrays.stream(Objects.requireNonNull(file.listFiles())).map(File::delete).toArray(Boolean[]::new);
return Arrays.asList(results).contains(false); // return Arrays.asList(results).contains(false);
}else{ // }else{
return file.delete(); // return file.delete();
} // }
} catch (Exception e) { // } catch (Exception e) {
System.out.println(e.getMessage()); // System.out.println(e.getMessage());
return false; // return false;
} // }
} // }
} // }
\ No newline at end of file \ No newline at end of file
...@@ -75,6 +75,6 @@ public class JWTokenUtil implements Serializable{ ...@@ -75,6 +75,6 @@ public class JWTokenUtil implements Serializable{
.parseClaimsJws(token) .parseClaimsJws(token)
.getBody() .getBody()
.getExpiration() .getExpiration()
.getTime() - new Date().getTime())/1000); .getTime() - new Date().getTime()));
} }
} }
\ No newline at end of file
...@@ -2,8 +2,8 @@ spring.datasource.url=jdbc:mysql://172.16.1.32:3306/seguridad ...@@ -2,8 +2,8 @@ spring.datasource.url=jdbc:mysql://172.16.1.32:3306/seguridad
spring.datasource.username=desarrollo spring.datasource.username=desarrollo
spring.datasource.password=5vC0$2019$ spring.datasource.password=5vC0$2019$
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
server.port=8081 server.port=8081
server.address=0.0.0.0 server.address=0.0.0.0
# spring.jpa.show-sql=true # spring.jpa.show-sql=true
app.jwtSecret=9a4f2c8d3b7a1e6f45c8a0b3f267d8b1d4e6f3c8a9d2b5f8e3a9c8b5f6v8a3d9 app.jwtSecret=9a4f2c8d3b7a1e6f45c8a0b3f267d8b1d4e6f3c8a9d2b5f8e3a9c8b5f6v8a3d9
spring.jpa.hibernate.ddl-auto = update spring.jpa.hibernate.ddl-auto = update
......
...@@ -8,6 +8,6 @@ ...@@ -8,6 +8,6 @@
<title>TRISMEGISTO-SERVICIOS</title> <title>TRISMEGISTO-SERVICIOS</title>
</head> </head>
<body> <body>
<h1>TRISMEGISTO-SERVICIOS</h1> <h1>TRISMEGISTO-SERVICES</h1>
</body> </body>
</html> </html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment