001package ball.upnp.ssdp;
002/*-
003 * ##########################################################################
004 * UPnP/SSDP Implementation Classes
005 * $Id: SSDPResponse.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/SSDPResponse.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.InetAddress;
025import java.util.regex.Pattern;
026import org.apache.http.Header;
027import org.apache.http.HttpVersion;
028import org.apache.http.message.BasicHttpResponse;
029import org.apache.http.message.BasicLineParser;
030
031/**
032 * SSDP {@link org.apache.http.HttpResponse} implementation.
033 *
034 * {@bean.info}
035 *
036 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
037 * @version $Revision: 5285 $
038 */
039public class SSDPResponse extends BasicHttpResponse implements SSDPMessage {
040    private static final BasicLineParser PARSER = BasicLineParser.INSTANCE;
041
042    private final InetAddress from;
043
044    /**
045     * Sole public constructor.
046     *
047     * @param   packet          The {@link DatagramPacket} containing the
048     *                          {@link SSDPResponse}.
049     */
050    public SSDPResponse(DatagramPacket packet) {
051        this(packet, toString(packet).split(Pattern.quote(CRLF)));
052    }
053
054    private SSDPResponse(DatagramPacket packet, String[] lines) {
055        super(BasicLineParser.parseStatusLine(lines[0], PARSER));
056
057        for (int i = 1; i < lines.length; i += 1) {
058            addHeader(BasicLineParser.parseHeader(lines[i], PARSER));
059        }
060
061        from = packet.getAddress();
062    }
063
064    private static String toString(DatagramPacket packet) {
065        return new String(packet.getData(),
066                          packet.getOffset(), packet.getLength(), CHARSET);
067    }
068
069    /**
070     * Protected constructor for subclasses.
071     *
072     * @param   code            The response status code.
073     * @param   reason          The response status reason.
074     */
075    protected SSDPResponse(int code, String reason) {
076        super(HttpVersion.HTTP_1_1, code, reason);
077
078        from = null;
079    }
080
081    /**
082     * Method to get the sender {@link InetAddress}.
083     *
084     * @return  The sender {@link InetAddress} (may be {@code null}).
085     */
086    public InetAddress getInetAddress() { return from; }
087
088    @Override
089    public String toString() {
090        StringBuilder buffer = new StringBuilder();
091
092        buffer.append(getStatusLine()).append(CRLF);
093
094        for (Header header : getAllHeaders()) {
095            buffer.append(header.toString()).append(CRLF);
096        }
097
098        buffer.append(CRLF);
099
100        return buffer.toString();
101    }
102}