提交 e3fab71c 编写于 作者: K kohsuke

eliminated the exception handling by using the Failure class.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@32055 71c3de6d-444a-0410-be80-ed276b4c234a
上级 b53c12d3
......@@ -28,7 +28,6 @@ import hudson.BulkChange;
import hudson.Util;
import hudson.XmlFile;
import hudson.model.AbstractModelObject;
import hudson.model.Failure;
import hudson.model.Hudson;
import hudson.model.Saveable;
import hudson.model.listeners.SaveableListener;
......@@ -43,7 +42,6 @@ import org.kohsuke.stapler.StaplerResponse;
import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Arrays;
import java.util.Locale;
......@@ -164,11 +162,7 @@ public class LogRecorder extends AbstractModelObject implements Saveable {
String newName = src.getString("name"), redirect = ".";
XmlFile oldFile = null;
if(!name.equals(newName)) {
try {
Hudson.checkGoodName(newName);
} catch (ParseException e) {
throw new Failure(e.getMessage());
}
Hudson.checkGoodName(newName);
oldFile = getConfigFile();
// rename
getParent().logRecorders.remove(name);
......
......@@ -44,7 +44,6 @@ import javax.servlet.ServletException;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
......@@ -74,7 +73,7 @@ public class LogRecorderManager extends AbstractModelObject {
return "/log";
}
public LogRecorder getDynamic(String token, StaplerRequest req, StaplerResponse rsp) {
public LogRecorder getDynamic(String token) {
return getLogRecorder(token);
}
......@@ -103,11 +102,7 @@ public class LogRecorderManager extends AbstractModelObject {
* Creates a new log recorder.
*/
public HttpResponse doNewLogRecorder(@QueryParameter String name) {
try {
Hudson.checkGoodName(name);
} catch (ParseException e) {
throw new Failure(e.getMessage());
}
Hudson.checkGoodName(name);
logRecorders.put(name,new LogRecorder(name));
......
......@@ -28,9 +28,9 @@ import hudson.XmlFile;
import hudson.Util;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.DependencyGraph;
import hudson.model.Descriptor;
import hudson.model.Failure;
import hudson.model.Hudson;
import hudson.model.Item;
import hudson.model.ItemGroup;
......@@ -60,7 +60,6 @@ import hudson.util.FormValidation;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
......@@ -557,15 +556,13 @@ public class MatrixProject extends AbstractProject<MatrixProject,MatrixBuild> im
if(req.getParameter("hasAxes")!=null) {
newAxes.addAll(req.bindParametersToList(Axis.class,"axis."));
// get rid of empty values
for (Iterator<Axis> itr = newAxes.iterator(); itr.hasNext();) try {
for (Iterator<Axis> itr = newAxes.iterator(); itr.hasNext();) {
Axis a = itr.next();
if(a.values.isEmpty()) { itr.remove(); continue; }
checkAxisName(a.name);
if (axisNames.contains(a.name))
throw new FormException(Messages.MatrixProject_DuplicateAxisName(),"axis.name");
axisNames.add(a.name);
} catch (ParseException e) {
throw new FormException(e.getMessage(),"axis.name");
}
}
......@@ -623,7 +620,7 @@ public class MatrixProject extends AbstractProject<MatrixProject,MatrixBuild> im
try {
checkAxisName(value);
return FormValidation.ok();
} catch (ParseException e) {
} catch (Failure e) {
return FormValidation.error(e.getMessage());
}
}
......@@ -632,7 +629,7 @@ public class MatrixProject extends AbstractProject<MatrixProject,MatrixBuild> im
* Makes sure that the given name is good as a axis name.
* TODO: maybe be even more restrictive since these are used as shell variables
*/
private static void checkAxisName(String name) throws ParseException {
private static void checkAxisName(String name) throws Failure {
Hudson.checkGoodName(name);
}
......
......@@ -43,7 +43,6 @@ import javax.servlet.ServletException;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.AbstractList;
import java.util.List;
import java.util.Map;
......@@ -212,15 +211,9 @@ public final class ComputerSet extends AbstractModelObject {
final Hudson app = Hudson.getInstance();
app.checkPermission(Hudson.ADMINISTER); // TODO: new permission?
try {
if(mode!=null && mode.equals("copy")) {
name = 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);
if(src==null) {
rsp.setStatus(SC_BAD_REQUEST);
......@@ -249,7 +242,7 @@ public final class ComputerSet extends AbstractModelObject {
}
NodeDescriptor d = NodeDescriptor.all().find(mode);
d.handleNewNodePage(this,req,rsp);
d.handleNewNodePage(this,name,req,rsp);
}
}
......@@ -259,35 +252,30 @@ public final class ComputerSet extends AbstractModelObject {
public synchronized void doDoCreateItem( StaplerRequest req, StaplerResponse rsp,
@QueryParameter String name,
@QueryParameter String type ) throws IOException, ServletException, FormException {
try {
final Hudson app = Hudson.getInstance();
app.checkPermission(Hudson.ADMINISTER); // TODO: new permission?
checkName(name);
final Hudson app = Hudson.getInstance();
app.checkPermission(Hudson.ADMINISTER); // TODO: new permission?
checkName(name);
Node result = NodeDescriptor.all().find(type).newInstance(req, req.getSubmittedForm());
app.addNode(result);
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);
}
// take the user back to the slave list top page
rsp.sendRedirect2(".");
}
/**
* Makes sure that the given name is good as a slave name.
* @return trimmed name if valid; throws ParseException if not
*/
private String checkName(String name) throws ParseException {
public String checkName(String name) throws Failure {
if(name==null)
throw new ParseException("Query parameter 'name' is required",0);
throw new Failure("Query parameter 'name' is required");
name = name.trim();
Hudson.checkGoodName(name);
if(Hudson.getInstance().getNode(name)!=null)
throw new ParseException(Messages.ComputerSet_SlaveAlreadyExists(name),0);
throw new Failure(Messages.ComputerSet_SlaveAlreadyExists(name));
// looks good
return name;
......@@ -305,7 +293,7 @@ public final class ComputerSet extends AbstractModelObject {
try {
checkName(value);
return FormValidation.ok();
} catch (ParseException e) {
} catch (Failure e) {
return FormValidation.error(e.getMessage());
}
}
......
......@@ -2540,13 +2540,7 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
return null;
}
try {
name = checkJobName(name);
} catch (ParseException e) {
rsp.setStatus(SC_BAD_REQUEST);
sendError(e,req,rsp);
return null;
}
name = checkJobName(name);
String mode = req.getParameter("mode");
if(mode!=null && mode.equals("copy")) {
......@@ -2653,12 +2647,8 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
}
public synchronized void doCreateView( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException, FormException {
try {
checkPermission(View.CREATE);
addView(View.create(req,rsp, this));
} catch (ParseException e) {
sendError(e,req,rsp);
}
checkPermission(View.CREATE);
addView(View.create(req,rsp, this));
}
/**
......@@ -2668,17 +2658,17 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
* @throws ParseException
* if the given name is not good
*/
public static void checkGoodName(String name) throws ParseException {
public static void checkGoodName(String name) throws Failure {
if(name==null || name.length()==0)
throw new ParseException(Messages.Hudson_NoName(),0);
throw new Failure(Messages.Hudson_NoName());
for( int i=0; i<name.length(); i++ ) {
char ch = name.charAt(i);
if(Character.isISOControl(ch)) {
throw new ParseException(Messages.Hudson_ControlCodeNotAllowed(toPrintableName(name)),i);
throw new Failure(Messages.Hudson_ControlCodeNotAllowed(toPrintableName(name)));
}
if("?*/\\%!@#$^&|<>[]:;".indexOf(ch)!=-1)
throw new ParseException(Messages.Hudson_UnsafeChar(ch),i);
throw new Failure(Messages.Hudson_UnsafeChar(ch));
}
// looks good
......@@ -2688,11 +2678,11 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
* Makes sure that the given name is good as a job name.
* @return trimmed name if valid; throws ParseException if not
*/
private String checkJobName(String name) throws ParseException {
private String checkJobName(String name) throws Failure {
checkGoodName(name);
name = name.trim();
if(getItem(name)!=null)
throw new ParseException(Messages.Hudson_JobAlreadyExists(name),0);
throw new Failure(Messages.Hudson_JobAlreadyExists(name));
// looks good
return name;
}
......@@ -3183,7 +3173,7 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
try {
checkJobName(value);
return FormValidation.ok();
} catch (ParseException e) {
} catch (Failure e) {
return FormValidation.error(e.getMessage());
}
}
......@@ -3299,7 +3289,7 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
* Checks if container uses UTF-8 to decode URLs. See
* http://hudson.gotdns.com/wiki/display/HUDSON/Tomcat#Tomcat-i18n
*/
public FormValidation doCheckURIEncoding(StaplerRequest request, StaplerResponse response) throws IOException {
public FormValidation doCheckURIEncoding(StaplerRequest request) throws IOException {
request.setCharacterEncoding("UTF-8");
// expected is non-ASCII String
final String expected = "\u57f7\u4e8b";
......
......@@ -66,7 +66,6 @@ import java.io.IOException;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
......@@ -1055,12 +1054,7 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R
String newName = req.getParameter("name");
if (newName != null && !newName.equals(name)) {
// check this error early to avoid HTTP response splitting.
try {
Hudson.checkGoodName(newName);
} catch (ParseException e) {
sendError(e, req, rsp);
return;
}
Hudson.checkGoodName(newName);
rsp.sendRedirect("rename?newName=" + URLEncoder.encode(newName, "UTF-8"));
} else {
rsp.sendRedirect(".");
......@@ -1316,12 +1310,7 @@ public abstract class Job<JobT extends Job<JobT, RunT>, RunT extends Run<JobT, R
checkPermission(DELETE);
String newName = req.getParameter("newName");
try {
Hudson.checkGoodName(newName);
} catch (ParseException e) {
sendError(e, req, rsp);
return;
}
Hudson.checkGoodName(newName);
if (isBuilding()) {
// redirect to page explaining that we can't rename now
......
......@@ -149,13 +149,9 @@ public class TreeView extends View implements ViewGroup {
}
public void doCreateView( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException, FormException {
try {
checkPermission(View.CREATE);
views.add(View.create(req,rsp,this));
save();
} catch (ParseException e) {
sendError(e,req,rsp);
}
checkPermission(View.CREATE);
views.add(View.create(req,rsp,this));
save();
}
// this feature is not public yet
......
......@@ -157,7 +157,7 @@ public abstract class View extends AbstractModelObject implements AccessControll
/**
* Renames this view.
*/
public void rename(String newName) throws ParseException, FormException {
public void rename(String newName) throws Failure, FormException {
if(name.equals(newName)) return; // noop
checkGoodName(newName);
if(owner.getView(newName)!=null)
......@@ -564,12 +564,7 @@ public abstract class View extends AbstractModelObject implements AccessControll
filterExecutors = req.getParameter("filterExecutors") != null;
filterQueue = req.getParameter("filterQueue") != null;
try {
rename(req.getParameter("name"));
} catch (ParseException e) {
sendError(e, req, rsp);
return;
}
rename(req.getParameter("name"));
owner.save();
......@@ -672,7 +667,7 @@ public abstract class View extends AbstractModelObject implements AccessControll
}
public static View create(StaplerRequest req, StaplerResponse rsp, ViewGroup owner)
throws ParseException, FormException, IOException, ServletException {
throws FormException, IOException, ServletException {
req.setCharacterEncoding("UTF-8");
String name = req.getParameter("name");
......
......@@ -23,6 +23,7 @@
*/
package hudson.slaves;
import hudson.Extension;
import hudson.model.ComputerSet;
import hudson.model.Descriptor;
import hudson.model.Slave;
......@@ -79,8 +80,12 @@ public abstract class NodeDescriptor extends Descriptor<Node> {
/**
* Handles the form submission from the "/computer/new" page, which is the first form for creating a new node.
* By default, it shows the configuration page for entering details, but subtypes can override this differently.
*
* @param name
* Name of the new node.
*/
public void handleNewNodePage(ComputerSet computerSet, StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
public void handleNewNodePage(ComputerSet computerSet, String name, StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
computerSet.checkName(name);
req.setAttribute("descriptor", this);
req.getView(computerSet,"_new.jelly").forward(req,rsp);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册