Search in sources :

Example 6 with GeneratedOutput

use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.

the class DefaultGeneratorPluginService method doGenerateWithApiVersion1.

private GeneratedOutput doGenerateWithApiVersion1(ModelInfo modelInfo, String serviceKey, Map<String, String> requestParams, String baseUrl) {
    if (modelInfo == null) {
        throw new ModelNotFoundException("Model with the given ID does not exist", null);
    }
    if (modelInfo.getType() == ModelType.Datatype || modelInfo.getType() == ModelType.Mapping) {
        throw new GenerationException("Provided model is neither an information model nor a function block model!");
    }
    HttpEntity<String> entity = getUserToken().map(token -> {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Bearer " + token);
        return new HttpEntity<>("parameters", headers);
    }).orElse(null);
    ModelId modelId = modelInfo.getId();
    ResponseEntity<byte[]> response = restTemplate.exchange(baseUrl + "/rest/generators/{pluginkey}/generate/{namespace}/{name}/{version}" + attachRequestParams(requestParams), HttpMethod.GET, entity, byte[].class, serviceKey, modelId.getNamespace(), modelId.getName(), modelId.getVersion());
    return new GeneratedOutput(response.getBody(), extractFileNameFromHeader(response), response.getHeaders().getContentLength());
}
Also used : Arrays(java.util.Arrays) GenerationException(org.eclipse.vorto.repository.plugin.generator.GenerationException) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) LoggerFactory(org.slf4j.LoggerFactory) IGeneratorPluginService(org.eclipse.vorto.repository.plugin.generator.IGeneratorPluginService) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ModelIdToModelContentConverter(org.eclipse.vorto.repository.conversion.ModelIdToModelContentConverter) ArrayList(java.util.ArrayList) IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Service(org.springframework.stereotype.Service) Map(java.util.Map) GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) IModelRepositoryFactory(org.eclipse.vorto.repository.core.IModelRepositoryFactory) RestTemplate(org.springframework.web.client.RestTemplate) OAuth2AuthenticationDetails(org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) HttpHeaders(org.springframework.http.HttpHeaders) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(org.springframework.http.HttpMethod) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ModelId(org.eclipse.vorto.model.ModelId) ModelType(org.eclipse.vorto.model.ModelType) GeneratorPluginConfiguration(org.eclipse.vorto.repository.plugin.generator.GeneratorPluginConfiguration) List(java.util.List) ModelContent(org.eclipse.vorto.model.ModelContent) HttpEntity(org.springframework.http.HttpEntity) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) Comparator(java.util.Comparator) Authentication(org.springframework.security.core.Authentication) GeneratorPluginInfo(org.eclipse.vorto.plugin.generator.GeneratorPluginInfo) HttpHeaders(org.springframework.http.HttpHeaders) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput) GenerationException(org.eclipse.vorto.repository.plugin.generator.GenerationException) ModelId(org.eclipse.vorto.model.ModelId)

Example 7 with GeneratedOutput

use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.

the class DefaultGeneratorPluginService method doGenerateWithApiVersion2.

private GeneratedOutput doGenerateWithApiVersion2(ModelId modelId, String serviceKey, Map<String, String> requestParams, String baseUrl) {
    ModelIdToModelContentConverter converter = new ModelIdToModelContentConverter(this.modelRepositoryFactory);
    ModelContent content = converter.convert(modelId, Optional.of(serviceKey));
    try {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Generating with V2. Sending following json content {}", new ObjectMapper().writeValueAsString(content));
        }
    } catch (JsonProcessingException e) {
        LOGGER.trace("Error processing JSON for logging", e);
    }
    ResponseEntity<byte[]> response = restTemplate.exchange(baseUrl + "/api/2/plugins/generators/{pluginkey}" + attachRequestParams(requestParams), HttpMethod.PUT, new HttpEntity<>(content), byte[].class, serviceKey);
    return new GeneratedOutput(response.getBody(), extractFileNameFromHeader(response), response.getHeaders().getContentLength());
}
Also used : ModelIdToModelContentConverter(org.eclipse.vorto.repository.conversion.ModelIdToModelContentConverter) ModelContent(org.eclipse.vorto.model.ModelContent) GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 8 with GeneratedOutput

use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.

the class GeneratedOutputAttachmentHandler method attachGeneratedOutput.

/**
 * This method creates a new attachment on the model with the given output of a generator.
 *
 * @param userContext   - The current user context
 * @param modelId       - the ID of the model used to invoke the generator
 * @param serviceKey    - the service key of the generator
 * @param requestParams - the parameters of the request
 * @param response      - the output of the generator
 * @param plugin        - the info about the invoked generator
 * @return the generated output that has been saved as attachment. The returned value has a
 * different file name than the one in the response parameter, as attachments of generator outputs
 * follow a naming convention.
 */
GeneratedOutput attachGeneratedOutput(IUserContext userContext, ModelId modelId, String serviceKey, Map<String, String> requestParams, GeneratedOutput response, GeneratorPluginConfiguration plugin) {
    String filename = buildAttachmentFilename(modelId, serviceKey, requestParams, response, plugin);
    FileContent fc = new FileContent(filename, response.getContent());
    modelRepositoryFactory.getRepositoryByModel(modelId, userContext).attachFileInElevatedSession(modelId, fc, userContext, GeneratedOutputAttachmentHandler.tagsForRequest(plugin, requestParams));
    return new GeneratedOutput(response.getContent(), filename, response.getSize());
}
Also used : FileContent(org.eclipse.vorto.repository.core.FileContent) GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput)

Aggregations

GeneratedOutput (org.eclipse.vorto.repository.plugin.generator.GeneratedOutput)8 ModelId (org.eclipse.vorto.model.ModelId)5 GeneratorPluginConfiguration (org.eclipse.vorto.repository.plugin.generator.GeneratorPluginConfiguration)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 ModelContent (org.eclipse.vorto.model.ModelContent)2 ModelIdToModelContentConverter (org.eclipse.vorto.repository.conversion.ModelIdToModelContentConverter)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1