All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mule.module.http.internal.multipart.HttpPart Maven / Gradle / Ivy

/*
 * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 * The software in this package is published under the terms of the CPAL v1.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE.txt file.
 */
package org.mule.module.http.internal.multipart;

import com.google.common.collect.Lists;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Part;

public class HttpPart implements Part
{

    public static final int NO_SIZE = -1;

    private final byte[] content;
    private final String contentType;
    private final String partName;
    private final String fileName;
    private final int size;
    private Map headers = new HashMap<>();

    public HttpPart(String partName, byte[] content, String contentType, int size)
    {
        this(partName, null, content, contentType, size);
    }

    public HttpPart(String partName, String fileName, byte[] content, String contentType, int size)
    {
        this.partName = partName;
        this.fileName = fileName;
        this.content = content;
        this.contentType = contentType;
        this.size = size;
    }

    @Override
    public void delete() throws IOException
    {
        throw new UnsupportedOperationException();
    }

    @Override
    public String getContentType()
    {
        return contentType;
    }

    @Override
    public String getHeader(String headerName)
    {
        return (String) headers.get(headerName);
    }

    @Override
    public Collection getHeaderNames()
    {
        return headers.keySet();
    }

    @Override
    public Collection getHeaders(String headerName)
    {
        Object headerValue = headers.get(headerName);

        if (headerValue instanceof  Collection)
        {
            return (Collection) headerValue;
        }
        else
        {
            return Lists.newArrayList((String) headerValue);
        }
    }

    public void addHeader(String headerName, String headerValue)
    {
        final Object value = headers.get(headerName);
        if (value == null)
        {
            headers.put(headerName, headerValue);
        }
        else
        {
            if (value instanceof Collection)
            {
                ((Collection) value).add(headerValue);
            }
            else
            {
                final ArrayList values = new ArrayList();
                values.add((String) value);
                values.add(headerValue);
                headers.put(headerName, values);
            }
        }
    }

    @Override
    public InputStream getInputStream() throws IOException
    {
        return new ByteArrayInputStream(content);
    }

    @Override
    public String getName()
    {
        return partName;
    }

    @Override
    public long getSize()
    {
        return size;
    }

    @Override
    public void write(String fileName) throws IOException
    {
        throw new UnsupportedOperationException();
    }

    public String getFileName()
    {
        return fileName;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy