org.openapitools.codegen.online.service.GenApiService Maven / Gradle / Ivy
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openapitools.codegen.online.service;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenConfigLoader;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.online.api.GenApiDelegate;
import org.openapitools.codegen.online.model.Generated;
import org.openapitools.codegen.online.model.GeneratorInput;
import org.openapitools.codegen.online.model.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
@Service
public class GenApiService implements GenApiDelegate {
private static List clients = new ArrayList<>();
private static List servers = new ArrayList<>();
private static Map fileMap = new HashMap<>();
static {
List extensions = CodegenConfigLoader.getAll();
for (CodegenConfig config : extensions) {
if (config.getTag().equals(CodegenType.CLIENT)
|| config.getTag().equals(CodegenType.DOCUMENTATION)) {
clients.add(config.getName());
} else if (config.getTag().equals(CodegenType.SERVER)) {
servers.add(config.getName());
}
}
clients.sort(String.CASE_INSENSITIVE_ORDER);
servers.sort(String.CASE_INSENSITIVE_ORDER);
}
@Autowired
private NativeWebRequest request;
@Override
public Optional getRequest() {
return Optional.ofNullable(request);
}
@Override
public ResponseEntity downloadFile(String fileId) {
Generated g = fileMap.get(fileId);
System.out.println("looking for fileId " + fileId);
System.out.println("got filename " + g.getFilename());
File file = new File(g.getFilename());
Path path = Paths.get(file.getAbsolutePath());
ByteArrayResource resource;
try {
resource = new ByteArrayResource(Files.readAllBytes(path));
} catch (FileNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "File not found", e);
} catch (IOException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "I/O error while reading file", e);
}
try {
FileUtils.deleteDirectory(file.getParentFile());
} catch (IOException e) {
System.out.println("failed to delete file " + file.getAbsolutePath());
}
return ResponseEntity
.ok()
.contentType(MediaType.valueOf("application/zip"))
.header("Content-Disposition",
"attachment; filename=\"" + g.getFriendlyName() + "-generated.zip\"")
.header("Accept-Range", "bytes")
//.header("Content-Length", bytes.length)
.body(resource);
}
@Override
public ResponseEntity generateClient(String language, GeneratorInput generatorInput) {
String filename = Generator.generateClient(language, generatorInput);
return getResponse(filename, language + "-client");
}
@Override
public ResponseEntity
© 2015 - 2024 Weber Informatics LLC | Privacy Policy