Skip to content

Commit

Permalink
Fix NPE in IndexerWebAccess and add missing ReflectionMarkers
Browse files Browse the repository at this point in the history
  • Loading branch information
theotherp committed Mar 23, 2024
1 parent ba04b6d commit 280fde3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
37 changes: 22 additions & 15 deletions core/src/main/java/org/nzbhydra/indexers/IndexerWebAccess.java
Expand Up @@ -5,6 +5,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.nzbhydra.Jackson;
import org.nzbhydra.config.ConfigProvider;
import org.nzbhydra.config.indexer.IndexerConfig;
Expand All @@ -25,6 +26,7 @@
import org.xml.sax.SAXParseException;

import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.StringReader;
import java.net.SocketTimeoutException;
import java.net.URI;
Expand Down Expand Up @@ -78,30 +80,19 @@ public <T> T get(URI uri, IndexerConfig indexerConfig, Class responseType) throw
if (responseType == String.class) {
return (T) response;
}
if (IndexerResponseTypeHolder.class.isAssignableFrom(responseType)) {
if (responseType != null && IndexerResponseTypeHolder.class.isAssignableFrom(responseType)) {
IndexerResponseTypeHolder responseTypeHolder = (IndexerResponseTypeHolder) responseType.getDeclaredConstructor().newInstance();
if (responseTypeHolder.getType() == IndexerResponseTypeHolder.ResponseType.JSON) {
return (T) Jackson.JSON_MAPPER.readValue(response, responseType);
} else if (responseTypeHolder.getType() == IndexerResponseTypeHolder.ResponseType.XML) {
try {
try (StringReader reader = new StringReader(response)) {
final StreamSource source = new StreamSource(reader);
T unmarshalled = (T) unmarshaller.unmarshal(source);
return unmarshalled;
}
} catch (UnmarshallingFailureException e) {
if (!response.toLowerCase().contains("function not available")) {
//Some indexers like Animetosho don't return a proper error code. This error may happen during caps check and we don't want to log it
logParseException(response, e);
}
throw new HydraUnmarshallingFailureException(e.getMessage(), e, response);
}
return unmarshalXml(response);

} else {
throw new RuntimeException("Unexpected responseTypeHolder type " + responseTypeHolder.getType());
}
}
throw new RuntimeException("Unexpected responseType " + responseType);
//Fall back to XML
return unmarshalXml(response);
});
} catch (RejectedExecutionException e) {
logger.error("Unexpected execution exception while executing call for indexer " + indexerConfig.getName() + ". This will hopefully be fixed soon", e);
Expand All @@ -127,6 +118,22 @@ public <T> T get(URI uri, IndexerConfig indexerConfig, Class responseType) throw
}
}

private <T> @NotNull T unmarshalXml(String response) throws IOException, HydraUnmarshallingFailureException {
try {
try (StringReader reader = new StringReader(response)) {
final StreamSource source = new StreamSource(reader);
T unmarshalled = (T) unmarshaller.unmarshal(source);
return unmarshalled;
}
} catch (UnmarshallingFailureException e) {
if (!response.toLowerCase().contains("function not available")) {
//Some indexers like Animetosho don't return a proper error code. This error may happen during caps check and we don't want to log it
logParseException(response, e);
}
throw new HydraUnmarshallingFailureException(e.getMessage(), e, response);
}
}

protected void logParseException(String response, UnmarshallingFailureException e) {
Optional<Throwable> saxParseExceptionOptional = Throwables.getCausalChain(e).stream().filter(x -> x instanceof SAXParseException).findFirst();
if (saxParseExceptionOptional.isPresent()) {
Expand Down
Expand Up @@ -18,11 +18,13 @@

import lombok.Data;
import org.nzbhydra.mapping.IndexerResponseTypeHolder;
import org.nzbhydra.springnative.ReflectionMarker;

import java.util.List;
import java.util.Map;

@Data
@ReflectionMarker
public class NzbIndexRoot implements IndexerResponseTypeHolder {

@Override
Expand All @@ -35,6 +37,7 @@ public ResponseType getType() {


@Data
@ReflectionMarker
public static class Stats {
private String query;
private boolean query_poster;
Expand All @@ -51,6 +54,7 @@ public static class Stats {
}

@Data
@ReflectionMarker
public static class Result {
private long id;
private String name;
Expand Down

0 comments on commit 280fde3

Please sign in to comment.