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