use of org.eclipse.vorto.repository.core.ModelInfo in project vorto by eclipse.
the class ModelRepositoryController method sendAsZipFile.
private void sendAsZipFile(final HttpServletResponse response, final String fileName, Map<ModelInfo, FileContent> models) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
try {
for (Map.Entry<ModelInfo, FileContent> model : models.entrySet()) {
ModelInfo modelResource = model.getKey();
ZipEntry zipEntry = new ZipEntry(modelResource.getId().getPrettyFormat() + modelResource.getType().getExtension());
zos.putNextEntry(zipEntry);
zos.write(model.getValue().getContent());
zos.closeEntry();
}
zos.close();
baos.close();
} catch (Exception ex) {
throw new GenericApplicationException("error in creating zip file.", ex);
}
response.setHeader(CONTENT_DISPOSITION, ATTACHMENT_FILENAME + fileName);
response.setContentType(APPLICATION_OCTET_STREAM);
try {
IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), response.getOutputStream());
response.flushBuffer();
} catch (IOException e) {
throw new GenericApplicationException("Error copying file.", e);
}
}
use of org.eclipse.vorto.repository.core.ModelInfo in project vorto by eclipse.
the class ModelRepositoryController method getModelForUI.
/**
* Fetches all data required to populate the returned {@link ModelFullDetailsDTO} (see class docs
* for details), in addition the model's "file" contents as file added to the response.<br/>
* Following error cases apply:
* <ul>
* <li>
* If {@link ModelId#fromPrettyFormat(String)} fails throwing {@link IllegalArgumentException},
* returns {@code null} with status {@link HttpStatus#NOT_FOUND}.
* </li>
* <li>
* If {@link ModelRepositoryController#getWorkspaceId(String)} fails throwing
* {@link FatalModelRepositoryException}, returns {@code null} with status
* {@link HttpStatus#NOT_FOUND}.
* </li>
* <li>
* If any operation such as:
* <ul>
* <li>
* {@link IModelRepository#getByIdWithPlatformMappings(ModelId)}
* </li>
* <li>
* {@link IModelRepository#getAttachments(ModelId)}
* </li>
* <li>
* {@link IModelPolicyManager#getPolicyEntries(ModelId)}
* </li>
* </ul>
* ... fails throwing {@link NotAuthorizedException}, returns {@code null} with status
* {@link HttpStatus#FORBIDDEN};
* </li>
* </ul>
*
* @param modelId
* @return
*/
@GetMapping("/ui/{modelId:.+}")
public ResponseEntity<ModelFullDetailsDTO> getModelForUI(@PathVariable String modelId, final HttpServletResponse response) {
try {
// resolve user
Authentication user = SecurityContextHolder.getContext().getAuthentication();
// resolve model ID
ModelId modelID = ModelId.fromPrettyFormat(modelId);
// resolve ModeShape workspace ID
String workspaceId = getWorkspaceId(modelId);
// fetches model info
ModelInfo modelInfo = getModelRepository(modelID).getByIdWithPlatformMappings(modelID);
if (Objects.isNull(modelInfo)) {
LOGGER.warn(String.format("Model resource with id [%s] not found. ", modelId));
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}
// starts spawning threads to retrieve models etc.
final ExecutorService executor = Executors.newCachedThreadPool();
// fetches mappings
Collection<ModelMinimalInfoDTO> mappings = ConcurrentHashMap.newKeySet();
modelInfo.getPlatformMappings().entrySet().stream().forEach(e -> {
executor.submit(new AsyncModelMappingsFetcher(mappings, e).with(SecurityContextHolder.getContext()).with(RequestContextHolder.getRequestAttributes()).with(getModelRepositoryFactory()));
});
// fetches references from model ids built with the root ModelInfo
Collection<ModelMinimalInfoDTO> references = ConcurrentHashMap.newKeySet();
modelInfo.getReferences().stream().forEach(id -> executor.submit(new AsyncModelReferenceFetcher(references, id).with(SecurityContextHolder.getContext()).with(RequestContextHolder.getRequestAttributes()).with(getModelRepositoryFactory())));
// fetches referenced by
Collection<ModelMinimalInfoDTO> referencedBy = ConcurrentHashMap.newKeySet();
modelInfo.getReferencedBy().stream().forEach(id -> executor.submit(new AsyncModelReferenceFetcher(referencedBy, id).with(SecurityContextHolder.getContext()).with(RequestContextHolder.getRequestAttributes()).with(getModelRepositoryFactory())));
// fetches attachments
Collection<Attachment> attachments = ConcurrentHashMap.newKeySet();
executor.submit(new AsyncModelAttachmentsFetcher(attachments, modelID, userRepositoryRoleService.isSysadmin(user.getName())).with(SecurityContextHolder.getContext()).with(RequestContextHolder.getRequestAttributes()).with(getModelRepositoryFactory()));
// fetches links
Collection<ModelLink> links = ConcurrentHashMap.newKeySet();
executor.submit(new AsyncModelLinksFetcher(modelID, links).with(SecurityContextHolder.getContext()).with(RequestContextHolder.getRequestAttributes()).with(getModelRepositoryFactory()));
// fetches available workflow actions
Collection<String> actions = ConcurrentHashMap.newKeySet();
executor.submit(new AsyncWorkflowActionsFetcher(workflowService, actions, modelID, UserContext.user(user, workspaceId)).with(SecurityContextHolder.getContext()).with(RequestContextHolder.getRequestAttributes()));
// fetches model syntax
Future<String> encodedSyntaxFuture = executor.submit(new AsyncModelSyntaxFetcher(modelID, SecurityContextHolder.getContext(), RequestContextHolder.getRequestAttributes(), getModelRepositoryFactory()));
// shuts down executor and waits for completion of tasks until configured timeout
// also retrieves callable content
executor.shutdown();
// single-threaded calls
// fetches policies in this thread
Collection<PolicyEntry> policies = getPolicyManager(workspaceId).getPolicyEntries(modelID).stream().filter(p -> userHasPolicyEntry(p, user, workspaceId)).collect(Collectors.toList());
// getting callables and setting executor timeout
String encodedSyntax = null;
try {
// callable content
encodedSyntax = encodedSyntaxFuture.get();
// timeout
if (!executor.awaitTermination(requestTimeoutInSeconds, TimeUnit.SECONDS)) {
LOGGER.warn(String.format("Requesting UI data for model ID [%s] took over [%d] seconds and programmatically timed out.", modelID, requestTimeoutInSeconds));
return new ResponseEntity<>(null, HttpStatus.GATEWAY_TIMEOUT);
}
} catch (InterruptedException ie) {
LOGGER.error("Awaiting executor termination was interrupted.");
return new ResponseEntity<>(null, HttpStatus.SERVICE_UNAVAILABLE);
} catch (ExecutionException ee) {
LOGGER.error("Failed to retrieve and encode model syntax asynchronously");
return new ResponseEntity<>(null, HttpStatus.SERVICE_UNAVAILABLE);
}
// builds DTO
ModelFullDetailsDTO dto = new ModelFullDetailsDTO().withModelInfo(modelInfo).withMappings(mappings).withReferences(references).withReferencedBy(referencedBy).withAttachments(attachments).withLinks(links).withActions(actions).withEncodedModelSyntax(encodedSyntax).withPolicies(policies);
return new ResponseEntity<>(dto, HttpStatus.OK);
}// could not resolve "pretty format" for given model ID
catch (IllegalArgumentException iae) {
LOGGER.warn(String.format("Could not resolve given model ID [%s]", modelId), iae);
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}// could not find namespace to resolve workspace ID from
catch (FatalModelRepositoryException fmre) {
LOGGER.warn(String.format("Could not resolve workspace ID from namespace inferred by model ID [%s]", modelId), fmre);
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
} catch (NotAuthorizedException nae) {
LOGGER.warn(String.format("Could not authorize fetching data from given model ID [%s] for calling user", modelId), nae);
return new ResponseEntity<>(null, HttpStatus.FORBIDDEN);
}
}
use of org.eclipse.vorto.repository.core.ModelInfo in project vorto by eclipse.
the class DefaultWorkflowService method getPossibleActions.
@Override
public List<String> getPossibleActions(ModelId model, IUserContext user) {
ModelInfo modelInfo = getModelRepository(user).getById(model);
Optional<IState> state = SIMPLE_WORKFLOW.getState(modelInfo.getState());
return state.get().getActions().stream().filter(action -> passesConditions(action.getConditions(), modelInfo, user)).map(IWorkflowElement::getName).collect(Collectors.toList());
}
use of org.eclipse.vorto.repository.core.ModelInfo in project vorto by eclipse.
the class DefaultWorkflowService method createDummyModelInfo.
private ModelInfo createDummyModelInfo(ModelId modelId, IUserContext user) {
// FIXME ?!?
ModelInfo model = new ModelInfo(modelId, ModelType.Functionblock);
model.setAuthor(user.getUsername());
return model;
}
use of org.eclipse.vorto.repository.core.ModelInfo in project vorto by eclipse.
the class HasPermissionEvaluator method hasPermission.
@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object targetPermission) {
final String username = authentication.getName();
if (targetDomainObject instanceof ModelId) {
if (targetPermission instanceof String) {
try {
ModelId modelId = (ModelId) targetDomainObject;
String workspaceId = namespaceService.resolveWorkspaceIdForNamespace(modelId.getNamespace()).orElseThrow(() -> new ModelNotFoundException("Model '" + modelId.getPrettyFormat() + "' can't be found in any workspace."));
String permission = (String) targetPermission;
ModelInfo modelInfo = repositoryFactory.getRepository(workspaceId, authentication).getById(modelId);
if (modelInfo != null) {
if ("model:delete".equalsIgnoreCase(permission)) {
return modelInfo.getAuthor().equalsIgnoreCase(username);
} else if ("model:get".equalsIgnoreCase(permission)) {
return modelInfo.getState().equals(SimpleWorkflowModel.STATE_RELEASED.getName()) || modelInfo.getState().equals(SimpleWorkflowModel.STATE_DEPRECATED.getName()) || modelInfo.getAuthor().equals(username);
} else if ("model:owner".equalsIgnoreCase(permission)) {
return modelInfo.getAuthor().equals(username);
}
}
} catch (NotAuthorizedException ex) {
return false;
}
} else if (targetPermission instanceof Permission) {
ModelId modelId = (ModelId) targetDomainObject;
Permission permission = (Permission) targetPermission;
String workspaceId = namespaceService.resolveWorkspaceIdForNamespace(modelId.getNamespace()).orElseThrow(() -> new ModelNotFoundException("The workspace for '" + modelId.getPrettyFormat() + "' could not be found."));
return repositoryFactory.getPolicyManager(workspaceId, authentication).hasPermission(modelId, permission);
}
} else if (targetDomainObject instanceof String) {
return username.equalsIgnoreCase((String) targetDomainObject);
}
return false;
}
Aggregations