001package ball.upnp.ssdp;
002/*-
003 * ##########################################################################
004 * UPnP/SSDP Implementation Classes
005 * $Id: SSDPRequest.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-upnp/trunk/src/main/java/ball/upnp/ssdp/SSDPRequest.java $
007 * %%
008 * Copyright (C) 2013 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import java.net.DatagramPacket;
024import java.net.InetSocketAddress;
025import java.util.regex.Pattern;
026import org.apache.http.Header;
027import org.apache.http.HttpVersion;
028import org.apache.http.message.BasicHttpRequest;
029import org.apache.http.message.BasicLineParser;
030
031/**
032 * SSDP {@link org.apache.http.HttpRequest} implementation.
033 *
034 * {@bean.info}
035 *
036 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
037 * @version $Revision: 5285 $
038 */
039public class SSDPRequest extends BasicHttpRequest implements SSDPMessage {
040    private static final BasicLineParser PARSER = BasicLineParser.INSTANCE;
041
042    /**
043     * Sole public constructor.
044     *
045     * @param   packet          The {@link DatagramPacket} containing the
046     *                          {@link SSDPRequest}.
047     */
048    public SSDPRequest(DatagramPacket packet) {
049        this(packet.getData(), packet.getOffset(), packet.getLength());
050    }
051
052    private SSDPRequest(byte[] data, int offset, int length) {
053        this(new String(data, offset, length, CHARSET)
054             .split(Pattern.quote(CRLF)));
055    }
056
057    private SSDPRequest(String[] lines) {
058        super(BasicLineParser.parseRequestLine(lines[0], PARSER));
059
060        for (int i = 1; i < lines.length; i += 1) {
061            addHeader(BasicLineParser.parseHeader(lines[i], PARSER));
062        }
063    }
064
065    /**
066     * Protected constructor for subclasses.
067     *
068     * @param   method          The name of the SSDP method.
069     */
070    protected SSDPRequest(String method) {
071        super(method, "*", HttpVersion.HTTP_1_1);
072    }
073
074    /**
075     * Convenience method to format a {@link InetSocketAddress} to its
076     * {@link String} representation.
077     *
078     * @param   address         The {@link InetSocketAddress}.
079     *
080     * @return  The {@link String} representation.
081     */
082    protected String toString(InetSocketAddress address) {
083        return address.getAddress().getHostAddress() + ":" + address.getPort();
084    }
085
086    @Override
087    public String toString() {
088        StringBuilder buffer = new StringBuilder();
089
090        buffer.append(getRequestLine()).append(CRLF);
091
092        for (Header header : getAllHeaders()) {
093            buffer.append(header.toString()).append(CRLF);
094        }
095
096        buffer.append(CRLF);
097
098        return buffer.toString();
099    }
100}