|
| 1 | +package com.github.javafaker; |
| 2 | + |
| 3 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | + |
| 7 | +/** |
| 8 | + * Faker class for generating Session Initiation Protocol (SIP) related data. |
| 9 | + * |
| 10 | + * @author TomerFi |
| 11 | + */ |
| 12 | +public final class Sip { |
| 13 | + private final Faker faker; |
| 14 | + private final ArrayList<Integer> portPool; |
| 15 | + |
| 16 | + protected Sip(final Faker faker) { |
| 17 | + this.faker = faker; |
| 18 | + int port = 40000; |
| 19 | + portPool = new ArrayList<Integer>(); |
| 20 | + while (port <= 50000) { |
| 21 | + portPool.add(port); |
| 22 | + port = port + 2; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * The various SIP methods are listed in https://en.wikipedia.org/wiki/Session_Initiation_Protocol. |
| 28 | + * |
| 29 | + * @return a SIP method String, e.g. {@code INVITE}. |
| 30 | + */ |
| 31 | + public String method() { |
| 32 | + return faker.resolve("sip.methods"); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Content types are based on https://tools.ietf.org/html/rfc5621 and |
| 37 | + * https://tools.ietf.org/html/rfc3261. |
| 38 | + * |
| 39 | + * @return a SIP content-type declaration String, e.g. {@code application/sdp} |
| 40 | + */ |
| 41 | + public String contentType() { |
| 42 | + return faker.resolve("sip.content.types"); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Get a 4 digit random port for SIP messaging. |
| 47 | + * |
| 48 | + * @return a SIP messaging port int, e.g. 5060. |
| 49 | + */ |
| 50 | + public int messagingPort() { |
| 51 | + return faker.random().nextInt(1000, 9999); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Get a 5 digit positive even port for rtp udp communication. |
| 56 | + * |
| 57 | + * @return an RTP UDP 5 digit port int, e.g. 40002. |
| 58 | + */ |
| 59 | + public int rtpPort() { |
| 60 | + return portPool.get(faker.random().nextInt(0, portPool.size())); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Proviosional code, the various response codes are listed in |
| 65 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 66 | + * |
| 67 | + * @return a 3 digit SIP provisioan response code between 100 and 199 int, e.g. {@code 180}. |
| 68 | + */ |
| 69 | + public int provisionalResponseCode() { |
| 70 | + return Integer.parseInt(faker.resolve("sip.response.codes.provisional")); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Success code, the various response codes are listed in |
| 75 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 76 | + * |
| 77 | + * @return a 3 digit SIP success response code between 200 and 299 int, e.g. {@code 200}. |
| 78 | + */ |
| 79 | + public int successResponseCode() { |
| 80 | + return Integer.parseInt(faker.resolve("sip.response.codes.success")); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Redirection code, the various response codes are listed in |
| 85 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 86 | + * |
| 87 | + * @return a 3 digit SIP redirection response code between 300 and 399 int, e.g. {@code 301}. |
| 88 | + */ |
| 89 | + public int redirectResponseCode() { |
| 90 | + return Integer.parseInt(faker.resolve("sip.response.codes.redirection")); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Client error code, the various response codes are listed in |
| 95 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 96 | + * |
| 97 | + * @return a 3 digit SIP client error response code between 400 and 499 int, e.g. {@code 486}. |
| 98 | + */ |
| 99 | + public int clientErrorResponseCode() { |
| 100 | + return Integer.parseInt(faker.resolve("sip.response.codes.clientError")); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Server error code, the various response codes are listed in |
| 105 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 106 | + * |
| 107 | + * @return a 3 digit SIP server error response code between 500 and 599 int, e.g. {@code 503}. |
| 108 | + */ |
| 109 | + public int serverErrorResponseCode() { |
| 110 | + return Integer.parseInt(faker.resolve("sip.response.codes.serverError")); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Global error code, the various response codes are listed in |
| 115 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 116 | + * |
| 117 | + * @return a 3 digit SIP global error response code between 600 and 699 int, e.g. {@code 608}. |
| 118 | + */ |
| 119 | + public int globalErrorResponseCode() { |
| 120 | + return Integer.parseInt(faker.resolve("sip.response.codes.globalError")); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Proviosional phrase, the various response phrases are listed in |
| 125 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 126 | + * |
| 127 | + * @return a SIP provisional response phrase String, e.g. {@code Ringing}. |
| 128 | + */ |
| 129 | + public String provisionalResponsePhrase() { |
| 130 | + return faker.resolve("sip.response.phrases.provisional"); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Success phrase, the various response phrases are listed in |
| 135 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 136 | + * |
| 137 | + * @return a SIP success response phrase String, e.g. {@code OK}. |
| 138 | + */ |
| 139 | + public String successResponsePhrase() { |
| 140 | + return faker.resolve("sip.response.phrases.success"); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Redirection phrase, the various response phrases are listed in |
| 145 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 146 | + * |
| 147 | + * @return a SIP redirection response phrase String, e.g. {@code Moved Permanently}. |
| 148 | + */ |
| 149 | + public String redirectResponsePhrase() { |
| 150 | + return faker.resolve("sip.response.phrases.redirection"); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Client error phrase, the various response phrases are listed in |
| 155 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 156 | + * |
| 157 | + * @return a SIP client error response phrase String, e.g. {@code Busy Here}. |
| 158 | + */ |
| 159 | + public String clientErrorResponsePhrase() { |
| 160 | + return faker.resolve("sip.response.phrases.clientError"); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Server error phrase, the various response phrases are listed in |
| 165 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 166 | + * |
| 167 | + * @return a SIP server erro response phrase String, e.g. {@code Service Unavailable}. |
| 168 | + */ |
| 169 | + public String serverErrorResponsePhrase() { |
| 170 | + return faker.resolve("sip.response.phrases.serverError"); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Server error phrase, the various response phrases are listed in |
| 175 | + * https://en.wikipedia.org/wiki/List_of_SIP_response_codes. |
| 176 | + * |
| 177 | + * @return a SIP global error response phrase String, e.g. {@code Rejected}. |
| 178 | + */ |
| 179 | + public String globalErrorResponsePhrase() { |
| 180 | + return faker.resolve("sip.response.phrases.globalError"); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Body example of SDP type can be found in https://tools.ietf.org/html/rfc5621. |
| 185 | + * |
| 186 | + * @return a fake SDP type SIP body String. |
| 187 | + */ |
| 188 | + public String bodyString() { |
| 189 | + return "v=0\n" + |
| 190 | + "o=" + faker.name().firstName() + " " + faker.internet().uuid() + " IN IP4 " + faker.internet().domainName() + "\n" + |
| 191 | + "s=-\n" + |
| 192 | + "c=IN IP4 " + faker.internet().ipV4Address() + "\n" + |
| 193 | + "t=0 0\n" + |
| 194 | + "m=audio " + rtpPort() + " RTP/AVP 0\n" + |
| 195 | + "a=rtpmap:0 PCMU/8000"; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Body example of SDP type can be found in https://tools.ietf.org/html/rfc5621. |
| 200 | + * |
| 201 | + * @return a fake SDP type SIP body byte array. |
| 202 | + */ |
| 203 | + public byte[] bodyBytes() { |
| 204 | + return bodyString().getBytes(UTF_8); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Return a valid name address to use with {@code to/from} headers. |
| 209 | + * |
| 210 | + * @return a valid name address String, e.g. {@code <sip:fakeName@10.1.2.3:5060>}. |
| 211 | + */ |
| 212 | + public String nameAddress() { |
| 213 | + return "<sip:" + faker.name().firstName() + "@" + faker.internet().ipV4Address() + ":" + messagingPort() + ">"; |
| 214 | + } |
| 215 | +} |
0 commit comments