| 1 | /**************************************************************** |
| 2 | ??*??Licensed??to??the??Apache??Software??Foundation??(ASF)??under??one??????* |
| 3 | ??*??or??more??contributor??license??agreements.????See??the??NOTICE??file??* |
| 4 | ??*??distributed??with??this??work??for??additional??information????????????????* |
| 5 | ??*??regarding??copyright??ownership.????The??ASF??licenses??this??file??????* |
| 6 | ??*??to??you??under??the??Apache??License,??Version??2.0??(the????????????????????????* |
| 7 | ??*??"License");??you??may??not??use??this??file??except??in??compliance??????* |
| 8 | ??*??with??the??License.????You??may??obtain??a??copy??of??the??License??at??????* |
| 9 | ??*????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????* |
| 10 | ??*??????http://www.apache.org/licenses/LICENSE-2.0??????????????????????????????????* |
| 11 | ??*????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????* |
| 12 | ??*??Unless??required??by??applicable??law??or??agreed??to??in??writing,??????* |
| 13 | ??*??software??distributed??under??the??License??is??distributed??on??an????* |
| 14 | ??*??"AS??IS"??BASIS,??WITHOUT??WARRANTIES??OR??CONDITIONS??OF??ANY??????????????* |
| 15 | ??*??KIND,??either??express??or??implied.????See??the??License??for??the????????* |
| 16 | ??*??specific??language??governing??permissions??and??limitations????????????* |
| 17 | ??*??under??the??License.??????????????????????????????????????????????????????????????????????????????????????* |
| 18 | ??****************************************************************/ |
| 19 | ?? |
| 20 | package??org.apache.james.mpt; |
| 21 | ?? |
| 22 | import??java.io.Reader; |
| 23 | import??java.io.StringReader; |
| 24 | ?? |
| 25 | /** |
| 26 | ??*??Adds??a??user??by??executing??a??script??at??a??port. |
| 27 | ??*??The??user??name??and??password??supplied??will??be??substituted?? |
| 28 | ??*??for??the??variables??<code>${user}</code>??and??<code>${password}</code>. |
| 29 | ??*/ |
| 30 | public??class??ScriptedUserAdder??implements??UserAdder??{ |
| 31 | ?? |
| 32 | ????????private??static??final??String??SCRIPT_NAME??=??"Add??User??Script"; |
| 33 | ????????private??static??final??String??PASSWORD_VARIABLE_NAME??=??"password"; |
| 34 | ????????private??static??final??String??USER_VARIABLE_NAME??=??"user"; |
| 35 | ???????? |
| 36 | ????????private??final??String??host; |
| 37 | ????????private??final??int??port; |
| 38 | ????????private??final??String??script; |
| 39 | ????????private??final??Monitor??monitor; |
| 40 | ???????? |
| 41 | ????????/** |
| 42 | ??????????*??Constructs??an??adder??without??a??script. |
| 43 | ??????????*??Note??that??{@link??#addUser(String,??String)}??will??not??be??available |
| 44 | ??????????*??@param??host??connect??to??this??host |
| 45 | ??????????*??@param??port??connect??to??this??port |
| 46 | ??????????*/ |
| 47 | ????????public??ScriptedUserAdder(final??String??host,??final??int??port) |
| 48 | ????????{ |
| 49 | ????????????????this(host,??port,??(String)??null); |
| 50 | ????????} |
| 51 | ???????? |
| 52 | ????????public??ScriptedUserAdder(final??String??host,??final??int??port,??final??String??script)??{ |
| 53 | ????????????????this(host,??port,??script,??new??NullMonitor()); |
| 54 | ????????} |
| 55 | ???????? |
| 56 | ????????/** |
| 57 | ??????????*??Note??that??{@link??#addUser(String,??String)}??will??not??be??available |
| 58 | ??????????*??@param??host??connect??to??this??host |
| 59 | ??????????*??@param??port??connect??to??this??port |
| 60 | ??????????*??@param??monitor??not??null |
| 61 | ??????????*/ |
| 62 | ????????public??ScriptedUserAdder(final??String??host,??final??int??port,??final??Monitor??monitor)??{ |
| 63 | ????????????????this(host,??port,??null,??monitor); |
| 64 | ????????} |
| 65 | ???????? |
| 66 | ????????public??ScriptedUserAdder(final??String??host,??final??int??port,??final??String??script,??final??Monitor??monitor)??{ |
| 67 | ????????????????this.host??=??host; |
| 68 | ????????????????this.port??=??port; |
| 69 | ????????????????this.script??=??script; |
| 70 | ????????????????this.monitor??=??monitor; |
| 71 | ????????} |
| 72 | ???????? |
| 73 | ????????/** |
| 74 | ??????????*??Adds??a??user??using??the??script??read??from??the??given??input. |
| 75 | ??????????*??@param??user??user??name,??not??null |
| 76 | ??????????*??@param??password??password??to??set,??not??null |
| 77 | ??????????*??@throws??Exception??upon??failure |
| 78 | ??????????*??@throws??NullPointerException??when??script??has??not??been??set |
| 79 | ??????????*/ |
| 80 | ????????public??void??addUser(final??String??user,??final??String??password)??throws??Exception??{ |
| 81 | ????????????????final??StringReader??reader??=??new??StringReader(script); |
| 82 | ????????????????addUser(user,??password,??reader); |
| 83 | ????????} |
| 84 | ?? |
| 85 | ????????/** |
| 86 | ??????????*??Adds??a??user??using??the??script??read??from??the??given??input. |
| 87 | ??????????*??@param??user??user??name,??not??null |
| 88 | ??????????*??@param??password??password??to??set,??not??null |
| 89 | ??????????*??@param??reader??reader??for??script,??not??null |
| 90 | ??????????*??@throws??Exception??upon??failure |
| 91 | ??????????*/ |
| 92 | ????????public??void??addUser(final??String??user,??final??String??password,??final??Reader??reader)??throws??Exception??{ |
| 93 | ????????????????final??ProtocolSessionBuilder??builder??=??new??ProtocolSessionBuilder(); |
| 94 | ????????????????builder.setVariable(USER_VARIABLE_NAME,??user); |
| 95 | ????????????????builder.setVariable(PASSWORD_VARIABLE_NAME,??password); |
| 96 | ???????????????? |
| 97 | ????????????????final??Runner??runner??=??new??Runner(); |
| 98 | ????????????????builder.addProtocolLines(SCRIPT_NAME,??reader,??runner.getTestElements()); |
| 99 | ????????????????final??ExternalSessionFactory??factory??=??new??ExternalSessionFactory(host,??port,??monitor,??null); |
| 100 | ????????????????runner.runSessions(factory); |
| 101 | ????????} |
| 102 | ?? |
| 103 | ????????/** |
| 104 | ??????????*??Constructs??a??<code>String</code>??with??all??attributes |
| 105 | ??????????*??in??name??=??value??format. |
| 106 | ??????????* |
| 107 | ??????????*??@return??a??<code>String</code>??representation?? |
| 108 | ??????????*??of??this??object. |
| 109 | ??????????*/ |
| 110 | ????????public??String??toString() |
| 111 | ????????{ |
| 112 | ????????????????final??String??TAB??=??"??"; |
| 113 | ???????????????? |
| 114 | ????????????????String??result??=??"ScriptedUserAdder??(??" |
| 115 | ????????????????????????+??super.toString()??+??TAB |
| 116 | ????????????????????????+??"host??=??"??+??this.host??+??TAB |
| 117 | ????????????????????????+??"port??=??"??+??this.port??+??TAB |
| 118 | ????????????????????????+??"script??=??"??+??this.script??+??TAB |
| 119 | ????????????????????????+??"monitor??=??"??+??this.monitor??+??TAB |
| 120 | ????????????????????????+??"??)"; |
| 121 | ???????? |
| 122 | ????????????????return??result; |
| 123 | ????????} |
| 124 | ???????? |
| 125 | ???????? |
| 126 | } |