1 16 package org.outerj.daisy.frontend; 17 18 import org.apache.cocoon.environment.Request; 19 import org.outerj.daisy.repository.Repository; 20 21 public class RequestUtil { 22 public static String getStringParameter(Request request, String paramName) throws Exception { 23 return getStringParameter(request, paramName, null); 24 } 25 26 public static String getStringParameter(Request request, String paramName, String defaultValue) throws Exception { 27 String value = request.getParameter(paramName); 28 29 if (value == null && defaultValue == null) { 30 throw new Exception ("Invalid request: parameter \"" + paramName + "\" is missing."); 31 } else if (value == null) { 32 return defaultValue; 33 } else { 34 return value; 35 } 36 } 37 38 public static int getIntParameter(Request request, String paramName, int defaultValue) throws Exception { 39 String value = request.getParameter(paramName); 40 41 if (value == null) { 42 return defaultValue; 43 } 44 45 try { 46 return Integer.parseInt(value); 47 } catch (NumberFormatException e) { 48 throw new Exception ("Invalid request: parameter \"" + paramName + "\" does not contain a valid integer value: \"" + value + "\"."); 49 } 50 } 51 52 public static long getLongParameter(Request request, String paramName) throws Exception { 53 String value = request.getParameter(paramName); 54 55 if (value == null) { 56 throw new Exception ("Invalid request: parameter \"" + paramName + "\" is missing."); 57 } 58 59 try { 60 return Long.parseLong(value); 61 } catch (NumberFormatException e) { 62 throw new Exception ("Invalid request: parameter \"" + paramName + "\" does not contain a valid long value: \"" + value + "\"."); 63 } 64 } 65 66 public static boolean getBooleanParameter(Request request, String paramName, boolean defaultValue) throws Exception { 67 String value = request.getParameter(paramName); 68 69 if (value == null) { 70 return defaultValue; 71 } else { 72 return value.equalsIgnoreCase("true"); 73 } 74 } 75 76 public static String getServer(Request request) { 77 String server = request.getScheme() + "://" + request.getServerName(); 78 if (request.getServerPort() != 80) 79 server += ":" + request.getServerPort(); 80 return server; 81 } 82 83 public static long getBranchId(Request request, long defaultBranchId, Repository repository) throws Exception { 84 String branchParam = request.getParameter("branch"); 85 return getBranchId(branchParam, defaultBranchId, repository); 86 } 87 88 public static long getBranchId(String branch, long defaultBranchId, Repository repository) throws Exception { 89 if (branch != null && branch.length() > 0) { 90 if (Character.isDigit(branch.charAt(0))) { 91 try { 92 return Long.parseLong(branch); 93 } catch (NumberFormatException e) { 94 throw new Exception ("Invalid branch specification: \"" + branch + "\"."); 95 } 96 } else { 97 return repository.getVariantManager().getBranch(branch, false).getId(); 98 } 99 } else { 100 return defaultBranchId; 101 } 102 } 103 104 public static long getLanguageId(Request request, long defaultLanguageId, Repository repository) throws Exception { 105 String languageParam = request.getParameter("language"); 106 return getLanguageId(languageParam, defaultLanguageId, repository); 107 } 108 109 public static long getLanguageId(String language, long defaultLanguageId, Repository repository) throws Exception { 110 if (language != null && language.length() > 0) { 111 if (Character.isDigit(language.charAt(0))) { 112 try { 113 return Long.parseLong(language); 114 } catch (NumberFormatException e) { 115 throw new Exception ("Invalid language specification: \"" + language + "\"."); 116 } 117 } else { 118 return repository.getVariantManager().getLanguage(language, false).getId(); 119 } 120 } else { 121 return defaultLanguageId; 122 } 123 } 124 125 129 public static String removePathFromUploadFileName(String fileName) { 130 int pos = fileName.lastIndexOf('\\'); 131 if (pos == -1) 132 pos = fileName.lastIndexOf('/'); 133 if (pos != -1) 134 fileName = fileName.substring(pos + 1); 135 return fileName; 136 } 137 138 } 139 | Popular Tags |