
org.jberet.support.io.RestItemWriter Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 2016 Red Hat, Inc. and/or its affiliates.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.jberet.support.io;
import java.io.Serializable;
import java.util.List;
import java.util.Locale;
import org.jberet.support._private.SupportMessages;
import jakarta.batch.api.BatchProperty;
import jakarta.batch.api.chunk.ItemWriter;
import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
/**
* An implementation of {@code ItemWriter} that posts data items to REST resource.
*
* Usage example:
*
* <chunk>
* ...
* <writer ref="restItemWriter">
* <properties>
* <property name="restUrl" value="http://localhost:8080/appName/rest-api/movies?param1=value1"/>
* </properties>
* </writer>
* </chunk>
*
*
* @see RestItemReader
* @see RestItemReaderWriterBase
*
* @since 1.3.0
*/
@Named
@Dependent
public class RestItemWriter extends RestItemReaderWriterBase implements ItemWriter {
/**
* Media type to use in the REST call to write data. Its value should be valid
* for the target REST resource. If not specified, this property defaults to
* {@value jakarta.ws.rs.core.MediaType#APPLICATION_JSON}.
*/
@Inject
@BatchProperty
protected String mediaType;
/**
* The {@code jakarta.ws.rs.core.MediaType} value based on {@link #mediaType}
* batch property. Its value is initialized in {@link #open(Serializable)}.
*/
protected MediaType mediaTypeInstance;
/**
* During the writer opening, the REST client is instantiated, and
* {@code checkpoint} is ignored.
*
* @param checkpoint checkpoint info ignored
* @throws Exception if error occurs
*/
@Override
public void open(final Serializable checkpoint) throws Exception {
super.open(checkpoint);
if(httpMethod == null) {
httpMethod = HttpMethod.POST;
} else {
httpMethod = httpMethod.toUpperCase(Locale.ENGLISH);
if (!HttpMethod.POST.equals(httpMethod) && !HttpMethod.PUT.equals(httpMethod)) {
throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, httpMethod, "httpMethod");
}
}
if (mediaType != null) {
mediaTypeInstance = MediaType.valueOf(mediaType);
} else {
mediaTypeInstance = MediaType.APPLICATION_JSON_TYPE;
}
}
@Override
public void writeItems(final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy