Search in sources :

Example 1 with Part

use of org.glassfish.api.admin.Payload.Part in project Payara by payara.

the class MultipartProprietaryWriter method writeTo.

public void writeTo(final Payload.Outbound payload, final ParameterMap parameters, final ActionReport ar, final OutputStream os, final ContentTypeWriter contentTypeWriter) throws IOException {
    final String boundary = getBoundary();
    // Content-Type
    String ctType = "form-data";
    if (parameters == null || parameters.size() == 0) {
        ctType = "mixed";
    }
    contentTypeWriter.writeContentType("multipart", ctType, boundary);
    // Write content
    final Writer writer = new BufferedWriter(new OutputStreamWriter(os));
    // Parameters
    if (parameters != null) {
        for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
            for (String value : entry.getValue()) {
                writeParam(writer, os, boundary, entry.getKey(), value);
            }
        }
    }
    // ActionReport
    if (ar != null) {
        writeActionReport(writer, os, boundary, ar);
    }
    // Payload
    if (payload != null) {
        Iterator<Part> parts = payload.parts();
        while (parts.hasNext()) {
            writePayloadPart(writer, os, boundary, parts.next());
        }
    }
    // Write the final boundary string
    multiWrite(writer, BOUNDERY_DELIMIT, boundary, BOUNDERY_DELIMIT, EOL);
    writer.flush();
}
Also used : Part(org.glassfish.api.admin.Payload.Part) OutputStreamWriter(java.io.OutputStreamWriter) List(java.util.List) ParameterMap(org.glassfish.api.admin.ParameterMap) Map(java.util.Map) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 2 with Part

use of org.glassfish.api.admin.Payload.Part in project Payara by payara.

the class PayloadFilesManager method processPartsExtended.

/**
 * Returns all Files extracted from the Payload, treating each Part as a
 * separate file, via a Map from each File to its associated Properties.
 *
 * @param inboundPayload Payload containing file data to be extracted
 * @return map from each extracted File to its corresponding Properties
 * @throws java.io.IOException
 */
public Map<File, Properties> processPartsExtended(final Payload.Inbound inboundPayload) throws Exception {
    if (inboundPayload == null) {
        return Collections.EMPTY_MAP;
    }
    final Map<File, Properties> result = new LinkedHashMap<File, Properties>();
    boolean isReportProcessed = false;
    Part possibleUnrecognizedReportPart = null;
    StringBuilder uploadedEntryNames = new StringBuilder();
    for (Iterator<Payload.Part> partIt = inboundPayload.parts(); partIt.hasNext(); ) {
        Payload.Part part = partIt.next();
        DataRequestType drt = DataRequestType.getType(part);
        if (drt != null) {
            result.put(drt.processPart(this, part, part.getName()), part.getProperties());
            isReportProcessed |= (drt == DataRequestType.REPORT);
            uploadedEntryNames.append(part.getName()).append(" ");
        } else {
            if ((!isReportProcessed) && possibleUnrecognizedReportPart == null) {
                possibleUnrecognizedReportPart = part;
            }
        }
    }
    if ((!isReportProcessed) && possibleUnrecognizedReportPart != null) {
        DataRequestType.REPORT.processPart(this, possibleUnrecognizedReportPart, possibleUnrecognizedReportPart.getName());
        isReportProcessed = true;
    }
    postProcessParts();
    return result;
}
Also used : Part(org.glassfish.api.admin.Payload.Part) Part(org.glassfish.api.admin.Payload.Part) Payload(org.glassfish.api.admin.Payload) Properties(java.util.Properties) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with Part

use of org.glassfish.api.admin.Payload.Part in project Payara by payara.

the class PayloadFilesManagerTest method testBraces.

@Test
public void testBraces() throws Exception {
    // //System.out.println("testBraces");
    final File tmpDir = File.createTempFile("gfpayl{braces}", "tmp");
    tmpDir.delete();
    tmpDir.mkdir();
    try {
        final PayloadFilesManager instance = new PayloadFilesManager.Perm(tmpDir, null, Logger.getAnonymousLogger());
        final String originalPath = "some/path";
        final Part testPart = PayloadImpl.Part.newInstance("text/plain", originalPath, null, "random content");
        final URI result = instance.getOutputFileURI(testPart, testPart.getName());
        // //System.out.println("  " + originalPath + " -> " + result);
        assertFalse(result.toASCIIString().contains("{"));
    } finally {
        PayloadFilesManagerTest.cleanup(tmpDir);
    }
}
Also used : Part(org.glassfish.api.admin.Payload.Part) File(java.io.File) URI(java.net.URI) Test(org.junit.Test)

Example 4 with Part

use of org.glassfish.api.admin.Payload.Part in project Payara by payara.

the class PayloadFilesManagerTest method testGetOutputFileURI.

/**
 * Test of getOutputFileURI method, of class PayloadFilesManager.
 */
@Test
public void testGetOutputFileURI() throws Exception {
    // //System.out.println("getOutputFileURI");
    PayloadFilesManager.Temp instance = new PayloadFilesManager.Temp(Logger.getAnonymousLogger());
    try {
        String originalPath = "way/over/there/myApp.ear";
        Part testPart = PayloadImpl.Part.newInstance("text/plain", originalPath, null, "random content");
        URI result = instance.getOutputFileURI(testPart, testPart.getName());
        // //System.out.println("  " + originalPath + " -> " + result);
        assertTrue(result.toASCIIString().endsWith("/myApp.ear"));
    } finally {
        instance.cleanup();
    }
}
Also used : Part(org.glassfish.api.admin.Payload.Part) URI(java.net.URI) Test(org.junit.Test)

Example 5 with Part

use of org.glassfish.api.admin.Payload.Part in project Payara by payara.

the class CheckpointHelper method loadOutbound.

private void loadOutbound(Outbound outbound, File outboundFile) throws IOException {
    if (outbound == null || !outboundFile.exists()) {
        return;
    }
    Inbound outboundSource = loadInbound(outboundFile);
    Iterator<Part> parts = outboundSource.parts();
    File topDir = createTempDir("checkpoint", "");
    topDir.deleteOnExit();
    while (parts.hasNext()) {
        Part part = parts.next();
        File sourceFile = File.createTempFile("source", "", topDir);
        FileUtils.copy(part.getInputStream(), new FileOutputStream(sourceFile), Long.MAX_VALUE);
        outbound.addPart(part.getContentType(), part.getName(), part.getProperties(), new FileInputStream(sourceFile));
    }
    outbound.resetDirty();
}
Also used : Part(org.glassfish.api.admin.Payload.Part) FileOutputStream(java.io.FileOutputStream) Inbound(org.glassfish.api.admin.Payload.Inbound) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

Part (org.glassfish.api.admin.Payload.Part)6 File (java.io.File)3 URI (java.net.URI)2 Test (org.junit.Test)2 BufferedWriter (java.io.BufferedWriter)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 ParameterMap (org.glassfish.api.admin.ParameterMap)1 Payload (org.glassfish.api.admin.Payload)1 Inbound (org.glassfish.api.admin.Payload.Inbound)1