PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/DataTurbine/com/rbnb/api/Username.java

https://bitbucket.org/OSDT/osdt-v3.2-production
Java | 596 lines | 164 code | 31 blank | 401 comment | 19 complexity | 28467472a42ee00106407857164111fb MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, GPL-2.0
  1. /*
  2. Copyright 2007 Creare Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.rbnb.api;
  14. /**
  15. * Username class for the <bold>RBNB V2</bold> DataTurbine server.
  16. * <p>
  17. *
  18. * @author Ian Brown
  19. *
  20. * @since V2.0
  21. * @version 04/17/2003
  22. */
  23. /*
  24. * Copyright 2003 Creare Inc.
  25. * All Rights Reserved
  26. *
  27. * Date By Description
  28. * MM/DD/YYYY
  29. * ---------- -- -----------
  30. * 04/17/2003 INB Added <code>allowAccess(Username)</code>.
  31. * 01/14/2003 INB Created.
  32. *
  33. */
  34. public final class Username
  35. extends com.rbnb.api.Serializable
  36. {
  37. /**
  38. * the password string or an indicator of how to get it.
  39. * <p>
  40. *
  41. * @author Ian Brown
  42. *
  43. * @since V2.0
  44. * @version 01/14/2003
  45. */
  46. private String password = null;
  47. /**
  48. * the username string.
  49. * <p>
  50. *
  51. * @author Ian Brown
  52. *
  53. * @since V2.0
  54. * @version 01/14/2003
  55. */
  56. private String username = null;
  57. // Class constants:
  58. private final static int PAR_PWD = 0;
  59. private final static int PAR_USN = 1;
  60. private final static String[] PARAMETERS = {
  61. "PWD",
  62. "USN"
  63. };
  64. /**
  65. * Class constructor.
  66. * <p>
  67. *
  68. * @author Ian Brown
  69. *
  70. * @since V2.0
  71. * @version 01/14/2003
  72. */
  73. /*
  74. *
  75. * Date By Description
  76. * MM/DD/YYYY
  77. * ---------- -- -----------
  78. * 01/14/2003 INB Created.
  79. *
  80. */
  81. public Username() {
  82. super();
  83. }
  84. /**
  85. * Class constructor to build a <code>Username</code> object from the
  86. * specified input streams.
  87. * <p>
  88. *
  89. * @author Ian Brown
  90. *
  91. * @param isI the control input stream.
  92. * @param disI the data input stream.
  93. * @exception com.rbnb.api.AddressException
  94. * thrown if there is a problem with an address.
  95. * @exception com.rbnb.api.AddressException
  96. * thrown if there is a problem with an address.
  97. * @exception com.rbnb.api.SerializeException
  98. * thrown if there is a problem with the serialization. For
  99. * example, a missing bracket.
  100. * @exception java.io.EOFException
  101. * thrown if the end of the input stream is reached.
  102. * @exception java.io.IOException
  103. * thrown if there is an error reading the input stream.
  104. * @exception java.lang.InterruptedException
  105. * thrown if the read is interrupted.
  106. * @since V2.0
  107. * @version 01/14/2003
  108. */
  109. /*
  110. *
  111. * Date By Description
  112. * MM/DD/YYYY
  113. * ---------- -- -----------
  114. * 01/14/2003 INB Created.
  115. *
  116. */
  117. Username(InputStream isI,DataInputStream disI)
  118. throws com.rbnb.api.AddressException,
  119. com.rbnb.api.SerializeException,
  120. java.io.EOFException,
  121. java.io.IOException,
  122. java.lang.InterruptedException
  123. {
  124. this();
  125. read(isI,disI);
  126. }
  127. /**
  128. * Class constructor to build a <code>Username</code> object from
  129. * username and password strings.
  130. * <p>
  131. *
  132. * @author Ian Brown
  133. *
  134. * @param usernameI the username string.
  135. * @param passwordI the password string.
  136. * @since V2.0
  137. * @version 01/14/2003
  138. */
  139. /*
  140. *
  141. * Date By Description
  142. * MM/DD/YYYY
  143. * ---------- -- -----------
  144. * 01/14/2003 INB Created.
  145. *
  146. */
  147. public Username(String usernameI,String passwordI) {
  148. this();
  149. setUsername(usernameI);
  150. setPassword(passwordI);
  151. }
  152. /**
  153. * Allow the specified <code>Username</code> access to this
  154. * <code>Username</code>?
  155. * <p>
  156. *
  157. * @author Ian Brown
  158. *
  159. * @param usernameI the <code>Username</code>.
  160. * @return is the <code>Username</code> allowed access?
  161. * @since V2.1
  162. * @version 04/17/2003
  163. */
  164. /*
  165. *
  166. * Date By Description
  167. * MM/DD/YYYY
  168. * ---------- -- -----------
  169. * 04/17/2003 INB Created.
  170. *
  171. */
  172. public final boolean allowAccess(Username usernameI) {
  173. boolean allowAccessR = true;
  174. String[] roles = getRoles();
  175. if ((roles != null) &&
  176. (roles.length > 0) &&
  177. ((roles.length > 1) || (roles[0].length() > 0))) {
  178. String[] otherRoles = ((usernameI == null) ?
  179. null :
  180. usernameI.getRoles());
  181. if (allowAccessR = (otherRoles != null)) {
  182. allowAccessR = false;
  183. for (int idx = 0;
  184. !allowAccessR && (idx < otherRoles.length);
  185. ++idx) {
  186. for (int idx1 = 0;
  187. !allowAccessR && (idx1 < roles.length);
  188. ++idx1) {
  189. allowAccessR = otherRoles[idx].equals(roles[idx1]);
  190. }
  191. }
  192. }
  193. }
  194. return (allowAccessR);
  195. }
  196. /**
  197. * Is this <code>Username</code> equal to the input one?
  198. * <p>
  199. *
  200. * @author Ian Brown
  201. *
  202. * @param otherI the other <code>Username</code>.
  203. * @return are the two equal?
  204. * @since V2.0
  205. * @version 01/14/2003a
  206. */
  207. /*
  208. *
  209. * Date By Description
  210. * MM/DD/YYYY
  211. * ---------- -- -----------
  212. * 01/14/2003 INB Created.
  213. *
  214. */
  215. public final boolean isEqual(Username otherI) {
  216. return (getUsername().equals(otherI.getUsername()) &&
  217. getPassword().equals(otherI.getPassword()));
  218. }
  219. /**
  220. * Gets the password string.
  221. * <p>
  222. *
  223. * @author Ian Brown
  224. *
  225. * @return the password string.
  226. * @see #setPassword(String passwordI)
  227. * @since V2.0
  228. * @version 01/14/2003
  229. */
  230. /*
  231. *
  232. * Date By Description
  233. * MM/DD/YYYY
  234. * ---------- -- -----------
  235. * 01/14/2003 INB Created.
  236. *
  237. */
  238. public final String getPassword() {
  239. return (password);
  240. }
  241. /**
  242. * Gets the list of roles granted to this <code>Username</code>.
  243. * <p>
  244. *
  245. * @author Ian Brown
  246. *
  247. * @return the list of roles.
  248. * @since V2.0
  249. * @version 01/14/2003
  250. */
  251. /*
  252. *
  253. * Date By Description
  254. * MM/DD/YYYY
  255. * ---------- -- -----------
  256. * 01/14/2003 INB Created.
  257. *
  258. */
  259. public final String[] getRoles() {
  260. String[] rolesR = null;
  261. if (getPassword() != null) {
  262. rolesR = new String[1];
  263. rolesR[0] = getPassword();
  264. }
  265. return (rolesR);
  266. }
  267. /**
  268. * Gets the username string.
  269. * <p>
  270. *
  271. * @author Ian Brown
  272. *
  273. * @return the username string.
  274. * @see #setUsername(String usernameI)
  275. * @since V2.0
  276. * @version 01/14/2003
  277. */
  278. /*
  279. *
  280. * Date By Description
  281. * MM/DD/YYYY
  282. * ---------- -- -----------
  283. * 01/14/2003 INB Created.
  284. *
  285. */
  286. public final String getUsername() {
  287. return (username);
  288. }
  289. /**
  290. * Reads the <code>Username</code> from the specified input stream.
  291. * <p>
  292. *
  293. * @author Ian Brown
  294. *
  295. * @param isI the input stream.
  296. * @param disI the data input stream.
  297. * @exception com.rbnb.api.AddressException
  298. * thrown if there is a problem with an address.
  299. * @exception com.rbnb.api.SerializeException
  300. * thrown if there is a problem with the serialization. For
  301. * example, a missing bracket.
  302. * @exception java.io.EOFException
  303. * thrown if the end of the input stream is reached.
  304. * @exception java.io.IOException
  305. * thrown if there is an error reading the input stream.
  306. * @exception java.lang.InterruptedException
  307. * thrown if this operation is interrupted.
  308. * @see #write(String[],int,com.rbnb.api.OutputStream,com.rbnb.api.DataOutputStream)
  309. * @since V2.0
  310. * @version 01/14/2003
  311. */
  312. /*
  313. *
  314. * Date By Description
  315. * MM/DD/YYYY
  316. * ---------- -- -----------
  317. * 01/14/2003 INB Created.
  318. *
  319. */
  320. final void read(InputStream isI,DataInputStream disI)
  321. throws com.rbnb.api.AddressException,
  322. com.rbnb.api.SerializeException,
  323. java.io.EOFException,
  324. java.io.IOException,
  325. java.lang.InterruptedException
  326. {
  327. // Read the open bracket marking the command of the
  328. // <code>Command</code>.
  329. Serialize.readOpenBracket(isI);
  330. int parameter;
  331. while ((parameter = Serialize.readParameter(PARAMETERS,isI)) != -1) {
  332. readStandardParameter(parameter,isI,disI);
  333. }
  334. }
  335. /**
  336. * Reads standard parameters.
  337. * <p>
  338. *
  339. * @author Ian Brown
  340. *
  341. * @param parameterI the parameter index.
  342. * @param isI the input stream.
  343. * @param disI the data input stream.
  344. * @return was the parameter recognized?
  345. * @exception com.rbnb.api.AddressException
  346. * thrown if there is a problem with an address.
  347. * @exception com.rbnb.api.SerializeException
  348. * thrown if there is a problem with the serialization. For
  349. * example, a missing bracket.
  350. * @exception java.io.EOFException
  351. * thrown if the end of the input stream is reached.
  352. * @exception java.io.IOException
  353. * thrown if there is an error reading the input stream.
  354. * @exception java.lang.InterruptedException
  355. * thrown if the read is interrupted.
  356. * @see #writeStandardParameters(com.rbnb.api.OutputStream,com.rbnb.api.DataOutputStream)
  357. * @since V2.0
  358. * @version 01/14/2003
  359. */
  360. /*
  361. *
  362. * Date By Description
  363. * MM/DD/YYYY
  364. * ---------- -- -----------
  365. * 01/14/2003 INB Created.
  366. *
  367. */
  368. final boolean readStandardParameter(int parameterI,
  369. InputStream isI,
  370. DataInputStream disI)
  371. throws com.rbnb.api.AddressException,
  372. com.rbnb.api.SerializeException,
  373. java.io.EOFException,
  374. java.io.IOException,
  375. java.lang.InterruptedException
  376. {
  377. boolean standardR = true;
  378. switch (parameterI) {
  379. case PAR_PWD:
  380. setPassword(isI.readUTF());
  381. break;
  382. case PAR_USN:
  383. setUsername(isI.readUTF());
  384. break;
  385. default:
  386. standardR = false;
  387. break;
  388. }
  389. return (standardR);
  390. }
  391. /**
  392. * Sets the password string.
  393. * <p>
  394. *
  395. * @author Ian Brown
  396. *
  397. * @param passwordI the password string.
  398. * @see #getPassword()
  399. * @since V2.0
  400. * @version 01/14/2003
  401. */
  402. /*
  403. *
  404. * Date By Description
  405. * MM/DD/YYYY
  406. * ---------- -- -----------
  407. * 01/14/2003 INB Created.
  408. *
  409. */
  410. public final void setPassword(String passwordI) {
  411. password = passwordI;
  412. }
  413. /**
  414. * Sets the username string.
  415. * <p>
  416. *
  417. * @author Ian Brown
  418. *
  419. * @param usernameI the username string.
  420. * @see #getUsername()
  421. * @since V2.0
  422. * @version 01/14/2003
  423. */
  424. /*
  425. *
  426. * Date By Description
  427. * MM/DD/YYYY
  428. * ---------- -- -----------
  429. * 01/14/2003 INB Created.
  430. *
  431. */
  432. public final void setUsername(String usernameI) {
  433. username = usernameI;
  434. }
  435. /**
  436. * Returns a string representation of this <code>Username</code>.
  437. * <p>
  438. *
  439. * @author Ian Brown
  440. *
  441. * @return the string representation.
  442. * @since V2.0
  443. * @version 01/14/2003
  444. */
  445. /*
  446. *
  447. * Date By Description
  448. * MM/DD/YYYY
  449. * ---------- -- -----------
  450. * 01/14/2003 INB Created.
  451. *
  452. */
  453. public final String toString() {
  454. return ("Username: " + getUsername());
  455. }
  456. /**
  457. * Writes this <code>Username</code> to the specified stream.
  458. * <p>
  459. *
  460. * @author Ian Brown
  461. *
  462. * @param parametersI our parent's parameter list.
  463. * @param paramterI the parent parameter to use.
  464. * @param osI the output stream.
  465. * @param dosI the data output stream.
  466. * @exception com.rbnb.api.AddressException
  467. * thrown if there is a problem with an address.
  468. * @exception com.rbnb.api.SerializeException
  469. * thrown if there is a problem with the serialization.
  470. * @exception java.io.IOException
  471. * thrown if there is a problem writing to the stream.
  472. * @exception java.lang.InterruptedException
  473. * thrown if the operation is interrupted.
  474. * @see #read(com.rbnb.api.InputStream,com.rbnb.api.DataInputStream)
  475. * @since V2.0
  476. * @version 01/14/2003
  477. */
  478. /*
  479. *
  480. * Date By Description
  481. * MM/DD/YYYY
  482. * ---------- -- -----------
  483. * 01/14/2003 INB Created.
  484. *
  485. */
  486. final void write(String[] parametersI,
  487. int parameterI,
  488. OutputStream osI,
  489. DataOutputStream dosI)
  490. throws com.rbnb.api.AddressException,
  491. com.rbnb.api.SerializeException,
  492. java.io.IOException,
  493. java.lang.InterruptedException
  494. {
  495. osI.writeParameter(parametersI,parameterI);
  496. Serialize.writeOpenBracket(osI);
  497. writeStandardParameters(osI,dosI);
  498. Serialize.writeCloseBracket(osI);
  499. }
  500. /**
  501. * Writes out standard parameters.
  502. * <p>
  503. *
  504. * @author Ian Brown
  505. *
  506. * @param osI the output stream.
  507. * @param dosI the data output stream.
  508. * @exception com.rbnb.api.AddressException
  509. * thrown if there is a problem with an address.
  510. * @exception com.rbnb.api.SerializeException
  511. * thrown if there is a problem with the serialization.
  512. * @exception java.io.EOFException
  513. * thrown if the end of the input stream is reached.
  514. * @exception java.io.IOException
  515. * thrown if there is an error during I/O.
  516. * @exception java.lang.InterruptedException
  517. * thrown if the operation is interrupted.
  518. * @see #readStandardParameter(int,com.rbnb.api.InputStream,com.rbnb.api.DataInputStream)
  519. * @since V2.0
  520. * @version 01/14/2003
  521. */
  522. /*
  523. *
  524. * Date By Description
  525. * MM/DD/YYYY
  526. * ---------- -- -----------
  527. * 01/14/2003 INB Created.
  528. *
  529. */
  530. final void writeStandardParameters(OutputStream osI,
  531. DataOutputStream dosI)
  532. throws com.rbnb.api.AddressException,
  533. com.rbnb.api.SerializeException,
  534. java.io.EOFException,
  535. java.io.IOException,
  536. java.lang.InterruptedException
  537. {
  538. if (getUsername() != null) {
  539. osI.writeParameter(PARAMETERS,PAR_USN);
  540. osI.writeUTF(getUsername());
  541. }
  542. if (getPassword() != null) {
  543. osI.writeParameter(PARAMETERS,PAR_PWD);
  544. osI.writeUTF(getPassword());
  545. }
  546. }
  547. }