You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2024/04/09 16:59:25 UTC
(jena) 04/06: Use switch expressions
This is an automated email from the ASF dual-hosted git repository.
andy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/jena.git
commit 613d42cc091744a230e3902a161cbea5fdc7b383
Author: Andy Seaborne <an...@apache.org>
AuthorDate: Sat Apr 6 12:04:26 2024 +0100
Use switch expressions
---
.../jena/fuseki/servlets/ActionProcessor.java | 19 ++--
.../apache/jena/fuseki/servlets/ActionREST.java | 55 +++++-----
.../apache/jena/fuseki/servlets/HttpAction.java | 22 ++--
.../apache/jena/fuseki/system/DataUploader.java | 119 +--------------------
.../fuseki/validation/html/ValidatorHtmlLib.java | 16 +--
5 files changed, 47 insertions(+), 184 deletions(-)
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionProcessor.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionProcessor.java
index b65101049c..caad104ec4 100644
--- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionProcessor.java
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionProcessor.java
@@ -30,16 +30,15 @@ public interface ActionProcessor {
*/
public default void process(HttpAction action) {
switch ( action.getMethod() ) {
- case METHOD_GET: execGet(action); break;
- case METHOD_POST: execPost(action); break;
- case METHOD_PATCH: execPatch(action); break;
- case METHOD_PUT: execPut(action); break;
- case METHOD_DELETE: execDelete(action); break;
- case METHOD_HEAD: execHead(action); break;
- case METHOD_OPTIONS: execOptions(action); break;
- case METHOD_TRACE: execTrace(action); break;
- // Unknown.
- default: execAny(action.getMethod(), action); break;
+ case METHOD_GET -> execGet(action);
+ case METHOD_POST -> execPost(action);
+ case METHOD_PATCH -> execPatch(action);
+ case METHOD_PUT -> execPut(action);
+ case METHOD_DELETE -> execDelete(action);
+ case METHOD_HEAD -> execHead(action);
+ case METHOD_OPTIONS-> execOptions(action);
+ case METHOD_TRACE -> execTrace(action);
+ default -> execAny(action.getMethod(), action);
}
}
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionREST.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionREST.java
index 44dc655d70..a362095c19 100644
--- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionREST.java
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/ActionREST.java
@@ -36,26 +36,17 @@ public abstract class ActionREST extends ActionService
public void execute(HttpAction action) {
// Intercept to put counters around calls.
String method = uppercase(action.getRequestMethod());
-
- if (method.equals(METHOD_GET))
- doGet$(action);
- else if (method.equals(METHOD_HEAD))
- doHead$(action);
- else if (method.equals(METHOD_POST))
- doPost$(action);
- else if (method.equals(METHOD_PATCH))
- doPatch$(action);
- else if (method.equals(METHOD_OPTIONS))
- doOptions$(action);
- else if (method.equals(METHOD_TRACE))
- //doTrace(action);
- ServletOps.errorMethodNotAllowed("TRACE");
- else if (method.equals(METHOD_PUT))
- doPut$(action);
- else if (method.equals(METHOD_DELETE))
- doDelete$(action);
- else
- ServletOps.errorNotImplemented("Unknown method: "+method);
+ switch(method) {
+ case METHOD_GET -> doGet$(action);
+ case METHOD_HEAD -> doHead$(action);
+ case METHOD_POST -> doPost$(action);
+ case METHOD_PATCH -> doPatch$(action);
+ case METHOD_PUT -> doPut$(action);
+ case METHOD_DELETE -> doDelete$(action);
+ case METHOD_OPTIONS -> doOptions$(action);
+ case METHOD_TRACE -> doTrace$(action);
+ default -> ServletOps.errorNotImplemented("Unknown method: "+method);
+ }
}
/**
@@ -145,16 +136,20 @@ public abstract class ActionREST extends ActionService
}
}
- protected abstract void doGet(HttpAction action);
- protected abstract void doHead(HttpAction action);
- protected abstract void doPost(HttpAction action);
- protected abstract void doPut(HttpAction action);
- protected abstract void doDelete(HttpAction action);
- protected abstract void doPatch(HttpAction action);
- protected abstract void doOptions(HttpAction action);
+ private final void doTrace$(HttpAction action) {
+ ServletOps.errorMethodNotAllowed("TRACE");
+ }
+
+ protected abstract void doGet(HttpAction action);
+ protected abstract void doHead(HttpAction action);
+ protected abstract void doPost(HttpAction action);
+ protected abstract void doPut(HttpAction action);
+ protected abstract void doDelete(HttpAction action);
+ protected abstract void doPatch(HttpAction action);
+ protected abstract void doOptions(HttpAction action);
- // If not final in ActionBase
- //@Override public void process(HttpAction action) { executeLifecycle(action); }
+ // If not final in ActionBase
+ @Override public void process(HttpAction action) { executeLifecycle(action); }
- @Override public void execAny(String methodName, HttpAction action) { executeLifecycle(action); }
+ @Override public void execAny(String methodName, HttpAction action) { executeLifecycle(action); }
}
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/HttpAction.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/HttpAction.java
index e55fee8c59..dc3bfd89d5 100644
--- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/HttpAction.java
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/servlets/HttpAction.java
@@ -647,20 +647,12 @@ public class HttpAction
String encoding = action.request.getHeader(HttpNames.hContentEncoding);
if ( encoding == null )
return input;
- switch (encoding) {
- case WebContent.encodingGzip :
- return new GZIPInputStream(input, 8192);
- case WebContent.encodingDeflate :
- return new DeflaterInputStream(input);
- // Not supported:
- case "br" :
- // From Apache Common Compress but needs extra org.brotli.dec.BrotliInputStream
- case "compress" :
- // Legacy - not supported
- default :
- }
- // Not supported or not understood.
- ServletOps.error(HttpSC.BAD_REQUEST_400, HttpNames.hContentEncoding+" '"+encoding+"' encoding not supported");
- return null;
+ return switch (encoding) {
+ case WebContent.encodingGzip -> new GZIPInputStream(input, 8192);
+ case WebContent.encodingDeflate -> new DeflaterInputStream(input);
+// case "br" :
+// case "compress" :
+ default-> { ServletOps.error(HttpSC.BAD_REQUEST_400, HttpNames.hContentEncoding+" '"+encoding+"' encoding not supported"); yield null; }
+ };
}
}
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/DataUploader.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/DataUploader.java
index 93e2edf152..321f6e72da 100644
--- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/DataUploader.java
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/system/DataUploader.java
@@ -103,16 +103,9 @@ public class DataUploader {
}
}
- // HttpServletRequests.getParts.
- // This operation combines the multi-part parsing and the handling the files.
- // Out code wishes to stream straigh to the destination graph or dataset.
+ // Previously, Jena has used HttpServletRequests.getParts.
// Each application server (Tomcat and Jetty) has special configuration.
- //
- // As of Jetty12, the config object is in the servlet holder and the code
- // has special handling for request.getAttribute("org.eclipse.jetty.multipartConfig");
- //
- // Fuseki is not a servlet!
- // Use Apache Commons FileUpload as the mulipart parser.
+ // Use Apache Commons FileUpload as the mulipart parser as it is portable.
/**
* Process an HTTP upload of RDF files (triples or quads) with content type
@@ -207,111 +200,3 @@ public class DataUploader {
}
}
}
-
-// // ----
-// // ---- This code is kept temporarily as we switch back to using Apache Commons FileUpload.
-//
-// // Version that works using HttpServletrequets.getParts.
-// // That means in Tomcat and Jetty, special configuration has to be done.
-// //
-// // Tomcat: In the WAR file: META-INF/context.xml
-// // <Context allowCasualMultipartParsing="true">
-// // </Context>
-// //
-// // At Jetty12, this no longer works.
-// //
-//
-// Apache Commons FileUploader is a portable solution
-//
-// // @MultipartConfig but for ServletFilters
-// // * @MultipartConfig only works on servlets.
-// // * Jetty: attribute setting.
-// // * Tomcat: META-INF/context.xml setting
-//
-// // Jetty requires a setting of this annotation object as a request attribute.
-// private static final MultipartConfigElement multipartConfigElement;
-// private static final String jettyMultipartAttributeName;
-//
-// static {
-// String x;
-// MultipartConfigElement y;
-// // May not be on the classpath
-// try {
-// Class.forName("org.eclipse.jetty.server.handler.ContextRequest");
-// //Portable across ee versions
-// x = org.eclipse.jetty.ee10.servlet.ServletContextRequest.MULTIPART_CONFIG_ELEMENT;
-// //x = "org.eclipse.jetty.multipartConfig";
-// y = new MultipartConfigElement("");
-// }
-// catch (ClassNotFoundException th) { x = null; y = null; }
-// jettyMultipartAttributeName = x;
-// multipartConfigElement = y;
-// }
-//
-// /**
-// * Process an HTTP upload of RDF files (triples or quads) with content type
-// * "multipart/form-data" or "multipart/mixed".
-// * <p>
-// * Data is streamed straight into the destination graph or dataset.
-// * <p>
-// * This function assumes it is inside a transaction.
-// */
-// private static UploadDetails multipartJakartaParts(HttpAction action, StreamRDF dest) {
-// HttpServletRequest request = action.getRequest();
-// String base = ActionLib.wholeRequestURL(request);
-// StreamRDFCounting countingDest = StreamRDFLib.count(dest);
-//
-// // Jetty
-// if ( jettyMultipartAttributeName != null && request.getAttribute(jettyMultipartAttributeName) == null ) {
-// request.setAttribute(jettyMultipartAttributeName, multipartConfigElement);
-// }
-//
-// // But the this goes to the Jetty Servlet holder, not the request attributes.
-// // Maybe there is a way to inject the object at build time.
-//
-// {
-// // This call is made internally to Jetty.
-// // This redirects to the servletHolder specifically for jettyMultipartAttributeName
-// // ServletContextRequest line 273: of getAttribute:
-// // case ServletContextRequest.MULTIPART_CONFIG_ELEMENT -> _matchedResource.getResource().getServletHolder().getMultipartConfigElement();
-//
-// //servletHolder.getRegistration().setMultipartConfig(multipartConfigElement);
-// // But then it fails because there no file directory to use (which we don't care about).
-//
-// Object x = request.getAttribute(jettyMultipartAttributeName);
-// }
-//
-// try {
-// for ( Part part : request.getParts() ) {
-// InputStream input = part.getInputStream();
-// boolean isFile = part.getHeader("Content-disposition") != null ;
-// if ( ! isFile ) {
-// // Form field - this code only supports multipart file upload.
-// String fieldName = part.getName();
-// InputStream stream = part.getInputStream();
-// String value = IO.readWholeFileAsUTF8(stream);
-// // This code is currently used to put multiple files into a single destination.
-// // Additional field/values do not make sense.
-// ServletOps.errorBadRequest(format("Only files accepted in multipart file upload (got %s=%s)", fieldName, value));
-// // errorBadRequest does not return.
-// return null;
-// }
-//
-// String contentTypeHeader = part.getContentType();
-// ContentType ct = ContentType.create(contentTypeHeader);
-// String partName = part.getName(); // AKA fieldName
-// String submittedFileName = part.getSubmittedFileName();
-//
-// handlePart(action, input, base, ct, submittedFileName, countingDest);
-// }
-// }
-// catch (ActionErrorException ex) { throw ex; }
-// catch (Exception ex) {
-// ex.printStackTrace();
-// ServletOps.errorOccurred(ex.getMessage());
-// }
-// // Overall results.
-// UploadDetails details = new UploadDetails(countingDest.count(), countingDest.countTriples(),countingDest.countQuads());
-// return details;
-// }
-
diff --git a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/html/ValidatorHtmlLib.java b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/html/ValidatorHtmlLib.java
index d0b0850888..f0bfddd8a5 100644
--- a/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/html/ValidatorHtmlLib.java
+++ b/jena-fuseki2/jena-fuseki-core/src/main/java/org/apache/jena/fuseki/validation/html/ValidatorHtmlLib.java
@@ -69,18 +69,10 @@ public class ValidatorHtmlLib {
for ( int i = 0; i < str.length(); i++ ) {
char ch = str.charAt(i);
switch (ch) {
- case '<' :
- sBuff.append("<");
- break;
- case '>' :
- sBuff.append(">");
- break;
- case '&' :
- sBuff.append("&");
- break;
- default :
- sBuff.append(ch);
- break;
+ case '<' -> sBuff.append("<");
+ case '>' -> sBuff.append(">");
+ case '&' -> sBuff.append("&");
+ default -> sBuff.append(ch);
}
}
return sBuff.toString();