Send Multipart Data
The following program provides a more detailed example that includes sending multipart data to a Media Server for processing.
Copy
import com.autonomy.aci.client.services.AciService;
import com.autonomy.aci.client.services.impl.AciServiceImpl;
import com.autonomy.aci.client.services.impl.DocumentProcessor;
import com.autonomy.aci.client.transport.AciServerDetails;
import com.autonomy.aci.client.transport.InputStreamActionParameter;
import com.autonomy.aci.client.transport.impl.AciHttpClientImpl;
import com.autonomy.aci.client.util.ActionParameters;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.w3c.dom.Document;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
public class MediaTest {
public static void main(String... args) throws TransformerException,
IOException {
// Create an AciService pointing at media server.
final AciService aciService = new AciServiceImpl(
new AciHttpClientImpl(HttpClients.createDefault()),
new AciServerDetails("mediaserver.example.com", 14000)
);
// Config for OCR, assumes [Channels] VisualChannels=1 is enabled on media
// server and that the relevant functionality is licensed.
final String config =
"[Session]\n" +
"Engine0=Ingest\n" +
"Engine1=OCR\n" +
"Engine2=Output\n" +
"\n" +
"[Ingest]\n" +
"Type=Image\n" +
"\n" +
"[OCR]\n" +
"Type=ocr\n" +
"Languages=en\n" +
"\n" +
"[Output]\n" +
"Type=response\n";
try (
final FileInputStream fis = new FileInputStream(
"/home/user/ocr_test.png"
)) {
final ActionParameters params = new ActionParameters("process");
// Plain string parameters can be sent directly.
params.add("synchronous", true);
// We can use input stream parameters for image files.
params.add(new InputStreamActionParameter("SourceData", fis));
// The documentation for 'Config' says it should either be base-64
// encoded or sent as multipart form data, so we're doing the
// latter below.
params.add(new InputStreamActionParameter("Config",
new ByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8))));
// Perform the request, and parse the response to XML.
final Document xml = aciService.executeAction(
params, new DocumentProcessor()
);
// Print the XML out.
TransformerFactory tf = TransformerFactory.newInstance();
final Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "2"
);
transformer.transform(new DOMSource(xml),
new StreamResult(new OutputStreamWriter(System.out, StandardCharsets.UTF_8)));
}
}
}