1 // Implemented by Anthony Kirby (anthony@anthony.org)
2 // based on SRVRecord.java Copyright (c) 1999-2004 Brian Wellington
3
4 package org.xbill.DNS;
5
6 import java.io.*;
7
8 /**
9 * Uniform Resource Identifier (URI) DNS Resource Record
10 *
11 * @author Anthony Kirby
12 * @see <a href="http://tools.ietf.org/html/draft-faltstrom-uri">http://tools.ietf.org/html/draft-faltstrom-uri</a>
13 */
14
15 public class URIRecord extends Record {
16
17 private static final long serialVersionUID = 7955422413971804232L;
18
19 private int priority, weight;
20 private byte[] target;
21
URIRecord()22 URIRecord() {
23 target = new byte[]{};
24 }
25
26 Record
getObject()27 getObject() {
28 return new URIRecord();
29 }
30
31 /**
32 * Creates a URI Record from the given data
33 * @param priority The priority of this URI. Records with lower priority
34 * are preferred.
35 * @param weight The weight, used to select between records at the same
36 * priority.
37 * @param target The host/port running the service
38 */
39 public
URIRecord(Name name, int dclass, long ttl, int priority, int weight, String target)40 URIRecord(Name name, int dclass, long ttl, int priority,
41 int weight, String target)
42 {
43 super(name, Type.URI, dclass, ttl);
44 this.priority = checkU16("priority", priority);
45 this.weight = checkU16("weight", weight);
46 try {
47 this.target = byteArrayFromString(target);
48 }
49 catch (TextParseException e) {
50 throw new IllegalArgumentException(e.getMessage());
51 }
52 }
53
54 void
rrFromWire(DNSInput in)55 rrFromWire(DNSInput in) throws IOException {
56 priority = in.readU16();
57 weight = in.readU16();
58 target = in.readByteArray();
59 }
60
61 void
rdataFromString(Tokenizer st, Name origin)62 rdataFromString(Tokenizer st, Name origin) throws IOException {
63 priority = st.getUInt16();
64 weight = st.getUInt16();
65 try {
66 target = byteArrayFromString(st.getString());
67 }
68 catch (TextParseException e) {
69 throw st.exception(e.getMessage());
70 }
71 }
72
73 /** Converts rdata to a String */
74 String
rrToString()75 rrToString() {
76 StringBuffer sb = new StringBuffer();
77 sb.append(priority + " ");
78 sb.append(weight + " ");
79 sb.append(byteArrayToString(target, true));
80 return sb.toString();
81 }
82
83 /** Returns the priority */
84 public int
getPriority()85 getPriority() {
86 return priority;
87 }
88
89 /** Returns the weight */
90 public int
getWeight()91 getWeight() {
92 return weight;
93 }
94
95 /** Returns the target URI */
96 public String
getTarget()97 getTarget() {
98 return byteArrayToString(target, false);
99 }
100
101 void
rrToWire(DNSOutput out, Compression c, boolean canonical)102 rrToWire(DNSOutput out, Compression c, boolean canonical) {
103 out.writeU16(priority);
104 out.writeU16(weight);
105 out.writeByteArray(target);
106 }
107
108 }
109