| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.webservice.common; |
| 18 |
|
| 19 |
import com.google.common.collect.Sets; |
| 20 |
|
| 21 |
import java.util.Set; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class Protocol { |
| 28 |
|
| 29 |
private Protocol() {} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public static interface ProtocolEnum { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public String getValue(); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public static enum RequestKey implements ProtocolEnum { |
| 47 |
CODE_URL("code_url"), |
| 48 |
JS_CODE("js_code"), |
| 49 |
EXCLUDE_DEFAULT_EXTERNS("exclude_default_externs"), |
| 50 |
EXTERNS_URL("externs_url"), |
| 51 |
EXTERNS_CODE("js_externs"), |
| 52 |
COMPILATION_LEVEL("compilation_level"), |
| 53 |
OUTPUT_FORMAT("output_format"), |
| 54 |
OUTPUT_INFO("output_info"), |
| 55 |
OUTPUT_FILE_NAME("output_file_name"), |
| 56 |
OUTPUT_WRAPPER("output_wrapper"), |
| 57 |
API_KEY("api_key"), |
| 58 |
FORMATTING("formatting"), |
| 59 |
WARNING_LEVEL("warning_level"), |
| 60 |
USER_ID("uid"), |
| 61 |
USE_CLOSURE("use_closure_library"), |
| 62 |
BUILD_DEBUG("debug"), |
| 63 |
CHARSET("charset"), |
| 64 |
LANGUAGE("language"), |
| 65 |
USE_TYPES_FOR_OPTIMIZATIONS("use_types_for_optimization"), |
| 66 |
|
| 67 |
|
| 68 |
RAWJS("rawjs"), |
| 69 |
BASE("base"), |
| 70 |
MODE("mode"), |
| 71 |
SCRIPT("script"), |
| 72 |
NOCACHE("nocache") |
| 73 |
; |
| 74 |
|
| 75 |
private static final Set<String> permittedKeys = getPermittedKeys(); |
| 76 |
|
| 77 |
private static Set<String> getPermittedKeys() { |
| 78 |
Set<String> keys = Sets.newHashSet(); |
| 79 |
|
| 80 |
for (RequestKey key : RequestKey.values()) { |
| 81 |
keys.add(key.asGetParameter()); |
| 82 |
} |
| 83 |
return keys; |
| 84 |
} |
| 85 |
|
| 86 |
private final String asGetParameter; |
| 87 |
|
| 88 |
private RequestKey(String asGetParameter) { |
| 89 |
this.asGetParameter = asGetParameter; |
| 90 |
} |
| 91 |
|
| 92 |
public String asGetParameter() { |
| 93 |
return asGetParameter; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public String toString() { |
| 98 |
return asGetParameter; |
| 99 |
} |
| 100 |
|
| 101 |
public static boolean isKeyValid(String key) { |
| 102 |
return permittedKeys.contains(key.toLowerCase()); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public String getValue() { |
| 107 |
return asGetParameter; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static enum OutputInfoKey implements ProtocolEnum { |
| 115 |
VARIABLE_MAP("variable_map"), |
| 116 |
COMPILED_CODE("compiled_code"), |
| 117 |
WARNINGS("warnings"), |
| 118 |
ERRORS("errors"), |
| 119 |
STATISTICS("statistics"), |
| 120 |
; |
| 121 |
|
| 122 |
private final String value; |
| 123 |
|
| 124 |
private OutputInfoKey(String value) { |
| 125 |
this.value = value; |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public String getValue() { |
| 130 |
return value; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public static enum FormattingKey implements ProtocolEnum { |
| 138 |
PRETTY_PRINT("pretty_print"), |
| 139 |
PRINT_INPUT_DELIMITER("print_input_delimiter"), |
| 140 |
; |
| 141 |
|
| 142 |
private final String value; |
| 143 |
|
| 144 |
private FormattingKey(String value) { |
| 145 |
this.value = value; |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public String getValue() { |
| 150 |
return value; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public static enum CompilationLevelKey implements ProtocolEnum { |
| 155 |
WHITESPACE_ONLY("whitespace_only"), |
| 156 |
SIMPLE_OPTIMIZATIONS("simple_optimizations"), |
| 157 |
ADVANCED_OPTIMIZATIONS("advanced_optimizations"), |
| 158 |
; |
| 159 |
|
| 160 |
private final String value; |
| 161 |
|
| 162 |
CompilationLevelKey(String value) { |
| 163 |
this.value = value; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public String getValue() { |
| 168 |
return value; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
public static enum WarningLevelKey implements ProtocolEnum { |
| 173 |
QUIET("quiet"), |
| 174 |
DEFAULT("default"), |
| 175 |
VERBOSE("verbose"), |
| 176 |
; |
| 177 |
|
| 178 |
private final String value; |
| 179 |
|
| 180 |
private WarningLevelKey(String value) { |
| 181 |
this.value = value; |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public String getValue() { |
| 186 |
return value; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
public static enum OutputFormatKey implements ProtocolEnum { |
| 191 |
TEXT("text"), |
| 192 |
XML("xml"), |
| 193 |
JSON("json"), |
| 194 |
; |
| 195 |
|
| 196 |
private final String value; |
| 197 |
|
| 198 |
private OutputFormatKey(String value) { |
| 199 |
this.value = value; |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public String getValue() { |
| 204 |
return value; |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public String toString() { |
| 209 |
return getValue(); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public static enum ApiKeyResponse implements ProtocolEnum { |
| 217 |
API_KEY("api_key"), |
| 218 |
; |
| 219 |
|
| 220 |
private final String responseParam; |
| 221 |
|
| 222 |
ApiKeyResponse(String responseParam) { |
| 223 |
this.responseParam = responseParam; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public String getResponseParam() { |
| 230 |
return responseParam; |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
public String toString() { |
| 235 |
return getResponseParam(); |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public String getValue() { |
| 240 |
return getResponseParam(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public static enum ResponseTag implements ProtocolEnum { |
| 249 |
ROOT_TAG("compilationResult"), |
| 250 |
COMPILED_CODE_TAG("compiledCode"), |
| 251 |
WARNINGS_TAG("warnings"), |
| 252 |
WARNING_TAG("warning"), |
| 253 |
ERRORS_TAG("errors"), |
| 254 |
ERROR_TAG("error"), |
| 255 |
ERROR_LINE_NO_ATTR("lineno"), |
| 256 |
ERROR_LINE_ATTR("line"), |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
ERROR_CHAR_ATTR("charno"), |
| 263 |
ERROR_FILE_ATTR("file"), |
| 264 |
ERROR_TYPE_ATTR("type"), |
| 265 |
STATS_TAG("statistics"), |
| 266 |
ORIGINAL_SIZE_TAG("originalSize"), |
| 267 |
ORIGINAL_GZIP_SIZE_TAG("originalGzipSize"), |
| 268 |
COMPRESSED_SIZE_TAG("compressedSize"), |
| 269 |
COMPRESSED_GZIP_SIZE_TAG("compressedGzipSize"), |
| 270 |
COMPILE_TIME_TAG("compileTime"), |
| 271 |
SERVER_ERRORS_TAG("serverErrors"), |
| 272 |
SERVER_ERROR_TAG("error"), |
| 273 |
SERVER_ERROR_CODE_ATTR("code"), |
| 274 |
VARIABLE_MAP("variableMap"), |
| 275 |
VARIABLE_MAP_ENTRY("variableMapEntry"), |
| 276 |
ORIGINAL_NAME_ATTR("originalName"), |
| 277 |
NEW_NAME_ATTR("newName"), |
| 278 |
OUTPUT_FILE_PATH("outputFilePath"), |
| 279 |
; |
| 280 |
|
| 281 |
private final String responseTag; |
| 282 |
|
| 283 |
private ResponseTag(String responseTag) { |
| 284 |
this.responseTag = responseTag; |
| 285 |
} |
| 286 |
|
| 287 |
public String getResponseTag() { |
| 288 |
return responseTag; |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public String toString() { |
| 293 |
return getResponseTag(); |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
public String getValue() { |
| 298 |
return getResponseTag(); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public static final String MAX_INPUT_SIZE_KEY = |
| 310 |
"com.google.javascript.jscomp.webservice.maximumInputSize"; |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public static final int FALLBACK_MAX_INPUT_SIZE = |
| 316 |
500 * 1024; |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
private static int maxInputSize; |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
static { |
| 329 |
resetMaximumInputSize(); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
public static final int maximumInputSize() { |
| 338 |
|
| 339 |
return maxInputSize; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public static final void resetMaximumInputSize() { |
| 348 |
String maxInputSizeStr = System.getProperty(Protocol.MAX_INPUT_SIZE_KEY); |
| 349 |
|
| 350 |
if (maxInputSizeStr == null) { |
| 351 |
maxInputSize = Protocol.FALLBACK_MAX_INPUT_SIZE; |
| 352 |
} else { |
| 353 |
maxInputSize = Integer.parseInt(maxInputSizeStr); |
| 354 |
} |
| 355 |
} |
| 356 |
} |