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

io.pkts.packet.sip.impl.SipRequestBuilder Maven / Gradle / Ivy

There is a newer version: 3.0.10
Show newest version
package io.pkts.packet.sip.impl;

import io.pkts.buffer.Buffer;
import io.pkts.buffer.Buffers;
import io.pkts.packet.sip.SipParseException;
import io.pkts.packet.sip.SipRequest;
import io.pkts.packet.sip.address.SipURI;
import io.pkts.packet.sip.address.URI;
import io.pkts.packet.sip.header.CSeqHeader;
import io.pkts.packet.sip.header.SipHeader;
import io.pkts.packet.sip.header.ToHeader;

import java.io.IOException;
import java.util.List;
import java.util.function.Function;

/**
 * @author [email protected]
 */
public class SipRequestBuilder extends SipMessageBuilder implements SipRequest.Builder {

    private Buffer method;

    private URI requestURI;

    public SipRequestBuilder(final Buffer method) {
        // TODO: do some research on how many headers an average request has.
        super(15);
        this.method = method;
    }

    @Override
    protected CSeqHeader generateDefaultCSeqHeader() {
        return CSeqHeader.withMethod(method).build();
    }

    @Override
    protected ToHeader generateDefaultToHeader() {
        PreConditions.assertNotNull(requestURI, "No request-uri has been specified so cannot generate a default To-header");
        if (requestURI.isSipURI()) {
            final SipURI sipURI = requestURI.toSipURI();
            return ToHeader.withHost(sipURI.getHost()).withUser(sipURI.getUser().orElse(null)).build();
        } else {
            throw new SipParseException("Not sure how to generate a default To-header off of a " + requestURI.getClass());
        }
    }

    @Override
    final protected boolean isBuildingRequest() {
        return true;
    }

    @Override
    protected SipInitialLine buildInitialLine() throws SipParseException {
        PreConditions.assertNotNull(requestURI, "You must specify the request URI");
        final Function f = getRequestURIFunction();
        URI finalURI = requestURI;
        if (finalURI.isSipURI() && f != null) {
            try {
                finalURI = f.apply(finalURI.toSipURI());
            } catch (final Exception e) {
                throw new SipParseException(0,
                        "Unable to construct request URI due exception from registered function", e);
            }
        }
        return new SipRequestLine(method, finalURI);
    }

    @Override
    protected SipRequest internalBuild(final Buffer msg,
                                       final SipInitialLine initialLine,
                                       final List headers,
                                       final short indexOfTo,
                                       final short indexOfFrom,
                                       final short indexOfCSeq,
                                       final short indexOfCallId,
                                       final short indexOfMaxForwards,
                                       final short indexOfVia,
                                       final short indexOfRoute,
                                       final short indexOfRecordRoute,
                                       final short indexOfContact,
                                       final Buffer body) {
        return new ImmutableSipRequest(msg, initialLine.toRequestLine(), headers,
                indexOfTo,
                indexOfFrom,
                indexOfCSeq,
                indexOfCallId,
                indexOfMaxForwards,
                indexOfVia,
                indexOfRoute,
                indexOfRecordRoute,
                indexOfContact,
                body);
    }

    @Override
    public SipRequest.Builder withRequestURI(final URI uri) throws SipParseException {
        PreConditions.assertNotNull(uri, "The request URI cannot be null");
        this.requestURI = uri;
        return this;
    }

    @Override
    public SipRequest.Builder withRequestURI(final String uri) throws SipParseException {
        PreConditions.assertNotEmpty(uri, "The URI cannot be empty string or null");
        try {
            this.requestURI = URI.frame(Buffers.wrap(uri));
        } catch (final IOException e) {
            // shouldn't really be able to happen
            throw new SipParseException("Received IOException when trying to read from the Buffer");
        }
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy