public static boolean isVoid(ComponentModel componentModel) { return componentModel instanceof HasOutputModel && ((HasOutputModel) componentModel).getOutput().getType() instanceof VoidType; }
/** * @param componentModel a {@link ComponentModel} * @return Whether the {@code componentModel} returns a list of messages */ public static boolean returnsListOfMessages(HasOutputModel componentModel) { MetadataType outputType = componentModel.getOutput().getType(); return outputType instanceof ArrayType && Message.class.getName().equals(getId(((ArrayType) outputType).getType()).orElse(null)); }
/** * This method returns a default {@link MediaType} given a {@link ComponentModel}. * * The first that applies will happen: * - If the component has a {@link MediaTypeModelProperty}, and this property has a MediaType, this one will be returned. * - If the compenent has an Output, a MediaType acording to this output will be returned. * - Otherwise, the ANY MediaType will be returned. * * @param componentModel {@link ComponentModel} that we want to get the defaultMediaType from. * @return the default {@link MediaType} according to the componentModel. */ public static MediaType getDefaultMediaType(ComponentModel componentModel) { Optional<MediaTypeModelProperty> mediaTypeModelProperty = componentModel.getModelProperty(MediaTypeModelProperty.class); if (mediaTypeModelProperty.isPresent() && mediaTypeModelProperty.get().getMediaType().isPresent()) { return mediaTypeModelProperty.get().getMediaType().get(); } if (componentModel instanceof HasOutputModel) { MetadataType output = ((HasOutputModel) componentModel).getOutput().getType(); return JAVA.equals(output.getMetadataFormat()) && output instanceof ObjectType ? MediaType.APPLICATION_JAVA : getMediaTypeFromMetadataType(output); } return ANY; }
public MapReturnHandler(HasOutputModel hasOutputModel) { MetadataType type = hasOutputModel.getOutput().getType(); Preconditions.checkArgument(isMap(type), "The given output type is not a Map"); mapDataType = (MapDataType) toDataType(type); }
/** * Creates a new instance * * @param componentModel the component which produces the return value * @param cursorProviderFactory the {@link CursorProviderFactory} to use when a message is doing cursor based streaming. Can be * {@code null} * @param muleContext the {@link MuleContext} of the owning application */ protected AbstractReturnDelegate(ComponentModel componentModel, CursorProviderFactory cursorProviderFactory, MuleContext muleContext) { if (componentModel instanceof HasOutputModel) { HasOutputModel hasOutputModel = (HasOutputModel) componentModel; returnsListOfMessages = returnsListOfMessages(hasOutputModel); MetadataType outputType = hasOutputModel.getOutput().getType(); if (isMap(outputType)) { isSpecialHandling = true; returnHandler = new MapReturnHandler(hasOutputModel); } else if (isJavaCollection(outputType)) { isSpecialHandling = true; returnHandler = new CollectionReturnHandler(outputType); } } this.muleContext = muleContext; this.cursorProviderFactory = cursorProviderFactory; defaultEncoding = getDefaultEncoding(muleContext); defaultMediaType = getDefaultMediaType(componentModel); }
private MetadataType adaptToListIfNecessary(MetadataType resolvedType, Object key, MetadataContext metadataContext) throws MetadataResolvingException { MetadataType componentOutputType = ((HasOutputModel) component).getOutput().getType(); if (!isCollection(componentOutputType) || isVoid(resolvedType) || isNullType(resolvedType)) { return resolvedType; } MetadataType collectionValueType = ((ArrayType) componentOutputType).getType(); Class<?> collectionType = getCollectionType(collectionValueType); if (Message.class.equals(collectionType)) { MessageMetadataType message = (MessageMetadataType) collectionValueType; resolvedType = wrapInMessageType(resolvedType, key, metadataContext, message.getAttributesType()); } return metadataContext.getTypeBuilder().arrayType() .with(new ClassInformationAnnotation(getCollectionType(componentOutputType))) .of(resolvedType) .build(); }
public static boolean isVoid(ComponentModel componentModel) { return componentModel instanceof HasOutputModel && ((HasOutputModel) componentModel).getOutput().getType() instanceof VoidType; }
/** * @param componentModel a {@link ComponentModel} * @return Whether the {@code componentModel} returns a list of messages */ public static boolean returnsListOfMessages(HasOutputModel componentModel) { MetadataType outputType = componentModel.getOutput().getType(); return outputType instanceof ArrayType && Message.class.getName().equals(getId(((ArrayType) outputType).getType()).orElse(null)); }
/** * Given a {@link MetadataKey} of a type and a {@link MetadataContext}, resolves the {@link MetadataType} of the Components's * output using the {@link OutputTypeResolver} associated to the current component. * * @param context {@link MetadataContext} of the Metadata resolution * @param key {@link MetadataKey} of the type which's structure has to be resolved * @return a {@link MetadataResult} with the {@link MetadataType} of the component's output */ private MetadataResult<MetadataType> getOutputMetadata(final MetadataContext context, final Object key) { OutputModel output = ((HasOutputModel) component).getOutput(); if (isVoid(output.getType()) || !output.hasDynamicType()) { return success(output.getType()); } try { MetadataType metadata = resolverFactory.getOutputResolver().getOutputType(context, key); if (isMetadataResolvedCorrectly(metadata, true)) { return success(adaptToListIfNecessary(metadata, key, context)); } MetadataFailure failure = newFailure() .withMessage("Error resolving Output Payload metadata") .withFailureCode(NO_DYNAMIC_TYPE_AVAILABLE) .withReason(NULL_TYPE_ERROR) .onOutputPayload(); return failure(output.getType(), failure); } catch (Exception e) { return failure(output.getType(), newFailure(e).onOutputPayload()); } }
returnsListOfMessages = returnsListOfMessages(hasOutputModel); MetadataType outputType = hasOutputModel.getOutput().getType(); .orElseGet(() -> { if (componentModel instanceof HasOutputModel) { MetadataType output = ((HasOutputModel) componentModel).getOutput().getType(); return JAVA.equals(output.getMetadataFormat()) && output instanceof ObjectType ? MediaType.APPLICATION_JAVA
public MapReturnHandler(HasOutputModel hasOutputModel) { MetadataType type = hasOutputModel.getOutput().getType(); Preconditions.checkArgument(isMap(type), "The given output type is not a Map"); mapDataType = (MapDataType) toDataType(type); mapKeyType = mapDataType.getKeyDataType().getType(); mapValueType = mapDataType.getValueDataType().getType(); }
private MetadataType adaptToListIfNecessary(MetadataType resolvedType, Object key, MetadataContext metadataContext) throws MetadataResolvingException { MetadataType componentOutputType = ((HasOutputModel) component).getOutput().getType(); if (!isCollection(componentOutputType) || isVoid(resolvedType) || isNullType(resolvedType)) { return resolvedType; } MetadataType collectionValueType = ((ArrayType) componentOutputType).getType(); Class<?> collectionType = getCollectionType(collectionValueType); if (Message.class.equals(collectionType)) { MessageMetadataType message = (MessageMetadataType) collectionValueType; resolvedType = wrapInMessageType(resolvedType, key, metadataContext, message.getAttributesType()); } return metadataContext.getTypeBuilder().arrayType() .with(new ClassInformationAnnotation(getCollectionType(componentOutputType))) .of(resolvedType) .build(); }
/** * Given a {@link MetadataKey} of a type and a {@link MetadataContext}, resolves the {@link MetadataType} of the Components's * output using the {@link OutputTypeResolver} associated to the current component. * * @param context {@link MetadataContext} of the Metadata resolution * @param key {@link MetadataKey} of the type which's structure has to be resolved * @return a {@link MetadataResult} with the {@link MetadataType} of the component's output */ private MetadataResult<MetadataType> getOutputMetadata(final MetadataContext context, final Object key) { OutputModel output = ((HasOutputModel) component).getOutput(); if (isVoid(output.getType()) || !output.hasDynamicType()) { return success(output.getType()); } try { MetadataType metadata = resolverFactory.getOutputResolver().getOutputType(context, key); if (isMetadataResolvedCorrectly(metadata, true)) { return success(adaptToListIfNecessary(metadata, key, context)); } MetadataFailure failure = newFailure() .withMessage("Error resolving Output Payload metadata") .withFailureCode(NO_DYNAMIC_TYPE_AVAILABLE) .withReason(NULL_TYPE_ERROR) .onOutputPayload(); return failure(output.getType(), failure); } catch (Exception e) { return failure(output.getType(), newFailure(e).onOutputAttributes()); } }