generated from QubitPi/jersey-webservice-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
285 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright Jiaqi Liu | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.qubitpi.wilhelm; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import net.jcip.annotations.Immutable; | ||
import net.jcip.annotations.ThreadSafe; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* A natural language represented in webservice that bridges the client request data format to database request format. | ||
*/ | ||
@Immutable | ||
@ThreadSafe | ||
public enum Language { | ||
|
||
/** | ||
* German language. | ||
*/ | ||
GERMAN("german", "German"), | ||
|
||
/** | ||
* Ancient Greek. | ||
*/ | ||
ANCIENT_GREEK("ancientGreek", "Ancient Greek"), | ||
|
||
/** | ||
* Latin. | ||
*/ | ||
LATIN("latin", "Latin"); | ||
|
||
private final String pathName; | ||
private final String databaseName; | ||
|
||
/** | ||
* All-args constructor. | ||
* | ||
* @param pathName The client-side language name | ||
* @param databaseName The database language name | ||
*/ | ||
Language(@NotNull final String pathName, @NotNull final String databaseName) { | ||
this.pathName = pathName; | ||
this.databaseName = databaseName; | ||
} | ||
|
||
/** | ||
* Constructs a {@link Language} from its client-side name. | ||
* | ||
* @param language The client-side requested language name | ||
* | ||
* @return a new instance | ||
* | ||
* @throws IllegalArgumentException if the language name is not a valid one | ||
*/ | ||
public static Language ofClientValue(@NotNull final String language) throws IllegalArgumentException { | ||
return Arrays.stream(values()) | ||
.filter(value -> value.pathName.equals(language)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException( | ||
String.format( | ||
"'%s' is not a recognized language. Acceptable ones are %s", | ||
language, | ||
Arrays.stream(values()).map(Language::getPathName).collect(Collectors.joining(", ") | ||
) | ||
))); | ||
} | ||
|
||
@NotNull | ||
public String getPathName() { | ||
return pathName; | ||
} | ||
|
||
@NotNull | ||
public String getDatabaseName() { | ||
return databaseName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright Jiaqi Liu | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.qubitpi.wilhelm; | ||
|
||
import jakarta.ws.rs.NameBinding; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* An annotation used exclusively by {@link org.qubitpi.wilhelm.web.filters.LanguageCheckFilter}. | ||
* | ||
* @see org.qubitpi.wilhelm.web.filters.LanguageCheckFilter | ||
*/ | ||
@NameBinding | ||
@Target({ElementType.METHOD, ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface LanguageCheck { | ||
|
||
// intentionally left blank | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/org/qubitpi/wilhelm/web/filters/LanguageCheckFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Jiaqi Liu | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.qubitpi.wilhelm.web.filters; | ||
|
||
import org.qubitpi.wilhelm.Language; | ||
import org.qubitpi.wilhelm.LanguageCheck; | ||
|
||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.container.ContainerRequestFilter; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.Provider; | ||
import net.jcip.annotations.Immutable; | ||
import net.jcip.annotations.ThreadSafe; | ||
|
||
/** | ||
* A {@link ContainerRequestFilter} that validates the {@code language} path param in endpoint requests and only applies | ||
* to those with "languages/{language}" in the middle of its endpoint path, for example "/languages/{language}/count". | ||
* <p> | ||
* Endpoints wishing to be validated by this filter must satisfy 2 requirements: | ||
* <ol> | ||
* <li> having a {@link jakarta.ws.rs.PathParam "language" @PathParam} | ||
* <li> the resource/endpoint method has been annotated with {@link LanguageCheck} | ||
* </ol> | ||
*/ | ||
@Immutable | ||
@ThreadSafe | ||
@Provider | ||
@LanguageCheck | ||
public class LanguageCheckFilter implements ContainerRequestFilter { | ||
|
||
@Override | ||
public void filter(final ContainerRequestContext containerRequestContext) { | ||
final String requestedLanguage = containerRequestContext | ||
.getUriInfo() | ||
.getPathParameters() | ||
.get("language") | ||
.get(0); | ||
try { | ||
Language.ofClientValue(requestedLanguage); | ||
} catch (final IllegalArgumentException exception) { | ||
containerRequestContext.abortWith( | ||
Response | ||
.status(Response.Status.BAD_REQUEST) | ||
.entity(exception.getMessage()) | ||
.build() | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.