Skip to content

Commit

Permalink
Reduce usage of jakarta.ws.rs.core.MultivaluedMap in the code in favo…
Browse files Browse the repository at this point in the history
…r of more generic java.util.Map
  • Loading branch information
sfuhrm committed Jan 7, 2024
1 parent fc688aa commit 55bb178
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.sfuhrm.radiobrowser4j;

import jakarta.ws.rs.core.MultivaluedMap;
import lombok.Builder;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/** Holder for an advanced search query.
Expand Down Expand Up @@ -97,73 +97,73 @@ public final class AdvancedSearch extends ParameterProvider {
private Boolean hideBroken;

@Override
protected void apply(final MultivaluedMap<String, String> requestParams) {
protected void apply(final Map<String, String> requestParams) {
if (name != null) {
requestParams.putSingle("name", name);
requestParams.put("name", name);
}
if (nameExact != null) {
requestParams.putSingle("nameExact", nameExact.toString());
requestParams.put("nameExact", nameExact.toString());
}
if (country != null) {
requestParams.putSingle("country", country);
requestParams.put("country", country);
}
if (countryExact != null) {
requestParams.putSingle("countryExact", countryExact.toString());
requestParams.put("countryExact", countryExact.toString());
}
if (countryCode != null) {
requestParams.putSingle("countrycode", countryCode);
requestParams.put("countrycode", countryCode);
}
if (state != null) {
requestParams.putSingle("state", state);
requestParams.put("state", state);
}
if (stateExact != null) {
requestParams.putSingle("stateExact", stateExact.toString());
requestParams.put("stateExact", stateExact.toString());
}
if (language != null) {
requestParams.putSingle("language", language);
requestParams.put("language", language);
}
if (languageExact != null) {
requestParams.putSingle("languageExact", languageExact.toString());
requestParams.put("languageExact", languageExact.toString());
}
if (tag != null) {
requestParams.putSingle("tag", tag);
requestParams.put("tag", tag);
}
if (tagExact != null) {
requestParams.putSingle("tagExact", tagExact.toString());
requestParams.put("tagExact", tagExact.toString());
}
if (tagList != null) {
requestParams.putSingle("tagList",
requestParams.put("tagList",
tagList.stream().collect(Collectors.joining(",")));
}
if (codec != null) {
requestParams.putSingle("codec", codec);
requestParams.put("codec", codec);
}
// TODO is the string mapping correct?
if (bitrateMin != null) {
requestParams.putSingle("bitrateMin", bitrateMin.toString());
requestParams.put("bitrateMin", bitrateMin.toString());
}
if (bitrateMax != null) {
requestParams.putSingle("bitrateMax", bitrateMax.toString());
requestParams.put("bitrateMax", bitrateMax.toString());
}
if (hasGeoInfo != null) {
requestParams.putSingle("has_geo_info", hasGeoInfo.toString());
requestParams.put("has_geo_info", hasGeoInfo.toString());
}
if (hasExtendedInfo != null) {
requestParams.putSingle("has_extended_info",
requestParams.put("has_extended_info",
hasExtendedInfo.toString());
}
if (isHttps != null) {
requestParams.putSingle("is_https", isHttps.toString());
requestParams.put("is_https", isHttps.toString());
}
if (order != null) {
requestParams.putSingle("order", order.name());
requestParams.put("order", order.name());
}
if (reverse != null) {
requestParams.putSingle("reverse", reverse.toString());
requestParams.put("reverse", reverse.toString());
}
// limit and offset are left out for Paging
if (hideBroken != null) {
requestParams.putSingle("hidebroken", reverse.toString());
requestParams.put("hidebroken", reverse.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import jakarta.ws.rs.core.MultivaluedMap;
import java.util.Collections;
import java.util.Map;

/** Parameters for list calls.
* @author Stephan Fuhrmann
Expand Down Expand Up @@ -72,15 +71,15 @@ public ListParameter reverseOrder(final boolean reverse) {
* Transfer this list parameter to the passed multi-valued-map.
* @param requestParams the target of the list params.
* */
protected void apply(final MultivaluedMap<String, String> requestParams) {
protected void apply(final Map<String, String> requestParams) {
log.info("list={}", this);
if (getOrder() != null) {
requestParams.put("order",
Collections.singletonList(getOrder().name().toLowerCase()));
getOrder().name().toLowerCase());
}
if (getReverseOrder() != null) {
requestParams.put("reverse", Collections.singletonList(
getReverseOrder().toString()));
requestParams.put("reverse",
getReverseOrder().toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
*/
package de.sfuhrm.radiobrowser4j;

import jakarta.ws.rs.core.MultivaluedMap;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.Map;

/** Immutable paging configuration.
* The paging is used to address a sub list that can be retrieved in
Expand Down Expand Up @@ -96,11 +95,11 @@ public Paging next() {
}

@Override
protected void apply(final MultivaluedMap<String, String> requestParams) {
protected void apply(final Map<String, String> requestParams) {
log.info("paging={}", this);
requestParams.put("limit", Collections.singletonList(
Integer.toString(getLimit())));
requestParams.put("offset", Collections.singletonList(
Integer.toString(getOffset())));
requestParams.put("limit",
Integer.toString(getLimit()));
requestParams.put("offset",
Integer.toString(getOffset()));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.sfuhrm.radiobrowser4j;

import jakarta.ws.rs.core.MultivaluedMap;
import java.util.Map;

/** A provider for HTTP request parameters. */
abstract class ParameterProvider {
Expand All @@ -9,5 +9,5 @@ abstract class ParameterProvider {
* @param requestParams the parameters to apply the instance
* content to.
* */
protected abstract void apply(MultivaluedMap<String, String> requestParams);
protected abstract void apply(Map<String, String> requestParams);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
import jakarta.ws.rs.core.GenericType;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -158,10 +158,7 @@ private Invocation.Builder builder(final WebTarget in) {
* */
private Map<String, Integer> retrieveValueStationCountList(
final String subPath) {
MultivaluedMap<String, String> requestParams =
new MultivaluedHashMap<>();

Entity<Form> entity = Entity.form(requestParams);
Entity<Form> entity = Entity.form(new MultivaluedHashMap<>());

try (Response response = builder(webTarget.path(subPath))
.post(entity)) {
Expand Down Expand Up @@ -225,12 +222,13 @@ private List<Station> listStationsPathWithPaging(
final Optional<Paging> paging,
final String path,
final ListParameter...listParam) {
MultivaluedMap<String, String> requestParams =
new MultivaluedHashMap<>();
Map<String, String> requestParams =
new HashMap<>();

paging.ifPresent(p -> p.apply(requestParams));
Arrays.stream(listParam).forEach(lp -> lp.apply(requestParams));
Entity<Form> entity = Entity.form(requestParams);
Entity<Form> entity = Entity.form(
new MultivaluedHashMap<>(requestParams));
try (Response response = builder(webTarget.path(path))
.post(entity)) {
checkResponseStatus(response);
Expand All @@ -251,11 +249,12 @@ private List<Station> listStationsPathWithLimit(
final Optional<Limit> limit,
final String path,
final ListParameter...listParam) {
MultivaluedMap<String, String> requestParams =
new MultivaluedHashMap<>();
Map<String, String> requestParams =
new HashMap<>();

Arrays.stream(listParam).forEach(lp -> lp.apply(requestParams));
Entity<Form> entity = Entity.form(requestParams);
Entity<Form> entity = Entity.form(
new MultivaluedHashMap<>(requestParams));
WebTarget target = webTarget.path(path);
if (limit.isPresent()) {
target = target.path(Integer.toString(limit.get().getSize()));
Expand Down Expand Up @@ -455,11 +454,12 @@ public List<Station> listStationsBy(@NonNull final Paging paging,
@NonNull final SearchMode searchMode,
@NonNull final String searchTerm,
final ListParameter...listParam) {
MultivaluedMap<String, String> requestParams =
new MultivaluedHashMap<>();
Map<String, String> requestParams =
new HashMap<>();
paging.apply(requestParams);
Arrays.stream(listParam).forEach(l -> l.apply(requestParams));
Entity<Form> entity = Entity.form(requestParams);
Entity<Form> entity = Entity.form(
new MultivaluedHashMap<>(requestParams));

try (Response response = builder(webTarget
.path("json/stations")
Expand All @@ -484,11 +484,12 @@ public Stream<Station> listStationsBy(
final ListParameter...listParam) {

Function<Paging, List<Station>> fetcher = p -> {
MultivaluedMap<String, String> requestParams =
new MultivaluedHashMap<>();
Map<String, String> requestParams =
new HashMap<>();
p.apply(requestParams);
Arrays.stream(listParam).forEach(l -> l.apply(requestParams));
Entity<Form> entity = Entity.form(requestParams);
Entity<Form> entity = Entity.form(
new MultivaluedHashMap<>(requestParams));

try (Response response = builder(webTarget
.path("json/stations")
Expand Down Expand Up @@ -590,11 +591,12 @@ public Stream<Station> listStationsWithAdvancedSearch(
@NonNull final AdvancedSearch advancedSearch) {

Function<Paging, List<Station>> fetcher = p -> {
MultivaluedMap<String, String> requestParams =
new MultivaluedHashMap<>();
Map<String, String> requestParams =
new HashMap<>();
p.apply(requestParams);
advancedSearch.apply(requestParams);
Entity<Form> entity = Entity.form(requestParams);
Entity<Form> entity = Entity.form(
new MultivaluedHashMap<>(requestParams));

try (Response response = builder(webTarget
.path("/json/stations/search"))
Expand Down Expand Up @@ -624,10 +626,11 @@ public Stream<Station> listStationsWithAdvancedSearch(
*/
private UUID postNewOrEditStation(@NonNull final Station station,
final String path) {
MultivaluedMap<String, String> requestParams =
new MultivaluedHashMap<>();
Map<String, String> requestParams =
new HashMap<>();
station.apply(requestParams);
Entity<Form> entity = Entity.form(requestParams);
Entity<Form> entity = Entity.form(
new MultivaluedHashMap<>(requestParams));

try (Response response = builder(webTarget
.path(path))
Expand Down
24 changes: 12 additions & 12 deletions radiobrowser4j/src/main/java/de/sfuhrm/radiobrowser4j/Station.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import jakarta.ws.rs.core.MultivaluedMap;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -30,6 +29,7 @@
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
Expand Down Expand Up @@ -238,39 +238,39 @@ public String toString() {
}

@Override
protected void apply(final MultivaluedMap<String, String> requestParams) {
requestParams.putSingle("name",
protected void apply(final Map<String, String> requestParams) {
requestParams.put("name",
getName());
requestParams.putSingle("url",
requestParams.put("url",
getUrl());
if (getHomepage() != null) {
requestParams.putSingle("homepage",
requestParams.put("homepage",
getHomepage());
}
if (getFavicon() != null) {
requestParams.putSingle("favicon",
requestParams.put("favicon",
getFavicon());
}
if (getCountryCode() != null) {
requestParams.putSingle("countrycode",
requestParams.put("countrycode",
getCountryCode());
}
if (getState() != null) {
requestParams.putSingle("state",
requestParams.put("state",
getState());
}
if (getLanguage() != null) {
requestParams.putSingle("language",
requestParams.put("language",
getLanguage());
}
if (getTagList() != null) {
requestParams.putSingle("tagList", getTags());
requestParams.put("tagList", getTags());
}
if (getGeoLatitude() != null) {
requestParams.putSingle("geo_lat", getGeoLatitude().toString());
requestParams.put("geo_lat", getGeoLatitude().toString());
}
if (getGeoLongitude() != null) {
requestParams.putSingle("geo_long", getGeoLongitude().toString());
requestParams.put("geo_long", getGeoLongitude().toString());
}
}
}
Loading

0 comments on commit 55bb178

Please sign in to comment.