提交 722d24ed 编写于 作者: M mindless

[FIXED HUDSON-3176] expose ComputerSet.checkName as a form field validator

and use it on new-slave page (was using Hudson.doItemExistsCheck which checks
against job names instead of slave names)


git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@15939 71c3de6d-444a-0410-be80-ed276b4c234a
上级 6aba618e
......@@ -27,6 +27,7 @@ import hudson.Util;
import hudson.slaves.NodeDescriptor;
import hudson.model.Descriptor.FormException;
import hudson.node_monitors.NodeMonitor;
import hudson.util.FormFieldValidator;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
......@@ -165,7 +166,13 @@ public final class ComputerSet extends AbstractModelObject {
final Hudson app = Hudson.getInstance();
app.checkPermission(Hudson.ADMINISTER); // TODO: new permission?
if (checkName(req, rsp, name)) return;
try {
checkName(name);
} catch (ParseException e) {
rsp.setStatus(SC_BAD_REQUEST);
sendError(e,req,rsp);
return;
}
if(mode!=null && mode.equals("copy")) {
Node src = app.getNode(from);
......@@ -208,14 +215,16 @@ public final class ComputerSet extends AbstractModelObject {
try {
final Hudson app = Hudson.getInstance();
app.checkPermission(Hudson.ADMINISTER); // TODO: new permission?
if (checkName(req, rsp, name)) return;
checkName(name);
Node result = NodeDescriptor.all().find(type).newInstance(req, req.getSubmittedForm());
app.addNode(result);
// take the user back to the slave list top page
rsp.sendRedirect2(".");
} catch (ParseException e) {
rsp.setStatus(SC_BAD_REQUEST);
sendError(e,req,rsp);
} catch (FormException e) {
sendError(e,req,rsp);
}
......@@ -224,27 +233,35 @@ public final class ComputerSet extends AbstractModelObject {
/**
* Makes sure that the given name is good as a slave name.
*/
private boolean checkName(StaplerRequest req, StaplerResponse rsp, String name) throws IOException, ServletException {
if(name==null) {
rsp.sendError(HttpServletResponse.SC_BAD_REQUEST,"Query parameter 'name' is required");
return true;
}
private void checkName(String name) throws ParseException {
if(name==null)
throw new ParseException("Query parameter 'name' is required",0);
name = name.trim();
Hudson.checkGoodName(name);
try {
Hudson.checkGoodName(name);
} catch (ParseException e) {
rsp.setStatus(SC_BAD_REQUEST);
sendError(e,req,rsp);
return true;
}
if(Hudson.getInstance().getNode(name)!=null)
throw new ParseException(Messages.ComputerSet_SlaveAlreadyExists(name),0);
if(Hudson.getInstance().getNode(name)!=null) {
rsp.setStatus(SC_BAD_REQUEST);
sendError(Messages.ComputerSet_SlaveAlreadyExists(name),req,rsp);
return true;
}
return false;
// looks good
}
/**
* Makes sure that the given name is good as a slave name.
*/
public void doCheckName(StaplerRequest req, StaplerResponse rsp, @QueryParameter final String value) throws IOException, ServletException {
new FormFieldValidator(req,rsp,Hudson.ADMINISTER) { // TODO: new permission?
protected void check() throws IOException, ServletException {
if(Util.fixEmpty(value)==null)
ok(); // nothing is entered yet
else try {
checkName(value);
ok();
} catch (ParseException e) {
error(e.getMessage());
}
}
}.process();
}
public Api getApi() {
......
......@@ -31,11 +31,11 @@ THE SOFTWARE.
-->
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:s="/lib/form">
<l:layout norefresh="true" permission="${app.ADMINISTER}">
<st:include page="sidepanel.jelly" />
<l:main-panel>
<st:include page="sidepanel.jelly" />
<l:main-panel>
<j:getStatic var="slaves" className="hudson.slaves.NodeDescriptor" field="ALL" />
<n:form nameTitle="${%Node name}" copyTitle="${%Copy Existing Node}" copyNames="${it._slaveNames}"
descriptors="${slaves}" xmlns:n="/lib/hudson/newFromList" />
descriptors="${slaves}" checkUrl="computer/checkName" xmlns:n="/lib/hudson/newFromList" />
</l:main-panel>
</l:layout>
</j:jelly>
\ No newline at end of file
</j:jelly>
......@@ -26,13 +26,13 @@ THE SOFTWARE.
"New Project" page.
-->
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:s="/lib/form">
<j:getStatic var="permission" className="hudson.model.Item" field="CREATE"/>
<l:layout norefresh="true" permission="${permission}">
<st:include page="sidepanel.jelly" />
<l:main-panel>
<j:getStatic var="permission" className="hudson.model.Item" field="CREATE"/>
<l:layout norefresh="true" permission="${permission}">
<st:include page="sidepanel.jelly" />
<l:main-panel>
<j:getStatic var="jobs" className="hudson.model.Items" field="LIST" />
<n:form nameTitle="${%Job name}" copyTitle="${%Copy existing job}" copyNames="${app.topLevelItemNames}"
descriptors="${jobs}" xmlns:n="/lib/hudson/newFromList" />
descriptors="${jobs}" checkUrl="itemExistsCheck" xmlns:n="/lib/hudson/newFromList" />
</l:main-panel>
</l:layout>
</j:jelly>
\ No newline at end of file
</l:layout>
</j:jelly>
......@@ -30,11 +30,12 @@ THE SOFTWARE.
@nameTitle : title of the name text field
@copyTitle : title of the copy option
@copyNames : options of names to choose from as the copy source. null to hide "copy" option
@checkUrl : form field validation url
-->
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:s="/lib/form">
<s:form method="post" action="createItem">
<s:entry title="${nameTitle}">
<s:textbox id="name" name="name" checkUrl="'${rootURL}/itemExistsCheck?value='+escape(this.value)"
<s:textbox id="name" name="name" checkUrl="'${rootURL}/${checkUrl}?value='+escape(this.value)"
onchange="updateOk(this.form)" onkeyup="updateOk(this.form)" />
<script>$('name').focus();</script>
</s:entry>
......@@ -88,4 +89,4 @@ THE SOFTWARE.
}
updateOk(okButton.getForm());
]]></script>
</j:jelly>
\ No newline at end of file
</j:jelly>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册