@Test public void shouldUseCorrectHeaders() throws Exception { NewznabJsonRoot jsonRoot = new NewznabJsonRoot(); when(newznabJsonTransformerMock.transformToRoot(any(), any(), anyInt(), any())).thenReturn(jsonRoot); NewznabParameters parameters = new NewznabParameters(); parameters.setQ("q1"); parameters.setApikey("apikey"); parameters.setT(ActionAttribute.SEARCH); parameters.setO(OutputType.JSON); ResponseEntity<?> responseEntity = testee.api(parameters, null, null); assertThat(responseEntity.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_JSON_UTF8); NewznabXmlRoot xmlRoot = new NewznabXmlRoot(); when(newznabXmlTransformerMock.getRssRoot(any(), any(), anyInt(), any())).thenReturn(xmlRoot); parameters.setO(OutputType.XML); responseEntity = testee.api(parameters, null, null); assertThat(responseEntity.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_XML); }
@RequestMapping(value = {"/api", "/rss", "/torznab/api", "/torznab/api/{indexerName}", "/api/{indexerName}"}, consumes = MediaType.ALL_VALUE) public ResponseEntity<? extends Object> api(NewznabParameters params, @PathVariable(value = "indexerName", required = false) String indexerName, @PathVariable(value = "mock", required = false) String mock) throws Exception { int searchRequestId = random.nextInt(100000); if (params.getT() != null && params.getT().isSearch()) { MDC.put("SEARCH", String.valueOf(searchRequestId)); } NewznabResponse.SearchType searchType = getSearchType(); logger.info("Received external {} API call: {}", searchType.name().toLowerCase(), params); if (!noApiKeyNeeded && !Objects.equals(params.getApikey(), configProvider.getBaseConfig().getMain().getApiKey())) { logger.error("Received API call with wrong API key"); throw new WrongApiKeyException("Wrong api key"); } if (!params.getIndexers().isEmpty() && indexerName != null) { logger.error("Received call with parameters set in path and request variables"); NewznabXmlError error = new NewznabXmlError("200", "Received call with parameters set in path and request variables"); return new ResponseEntity<Object>(error, HttpStatus.OK); } else if (indexerName != null) { params.setIndexers(Sets.newHashSet(indexerName)); } if (params.getT() == ActionAttribute.CAPS) { return capsGenerator.getCaps(params.getO(), searchType); } if (Stream.of(ActionAttribute.SEARCH, ActionAttribute.BOOK, ActionAttribute.TVSEARCH, ActionAttribute.MOVIE).anyMatch(x -> x == params.getT())) { if (inMockingMode) { logger.debug("Will mock results for this request"); return new ResponseEntity<>(mockSearch.mockSearch(params, getSearchType() == NewznabResponse.SearchType.NEWZNAB), HttpStatus.OK); } if (params.getCachetime() != null || configProvider.getBaseConfig().getSearching().getGlobalCacheTimeMinutes().isPresent()) { return handleCachingSearch(params, searchType, searchRequestId); } NewznabResponse searchResult = search(params, searchRequestId); HttpHeaders httpHeaders = setSearchTypeAndGetHeaders(params, searchResult); return new ResponseEntity<>(searchResult, httpHeaders, HttpStatus.OK); } if (params.getT() == ActionAttribute.GET) { return getNzb(params); } logger.error("Incorrect API request: {}", params); NewznabXmlError error = new NewznabXmlError("200", "Unknown or incorrect parameter"); return new ResponseEntity<Object>(error, HttpStatus.OK); }
ExternalApi { @RequestMapping(value = {"/api", "/rss", "/torznab/api", "/torznab/api/{indexerName}", "/api/{indexerName}"}, consumes = MediaType.ALL_VALUE) public ResponseEntity<? extends Object> api(NewznabParameters params, @PathVariable(value = "indexerName", required = false) String indexerName, @PathVariable(value = "mock", required = false) String mock) throws Exception { int searchRequestId = random.nextInt(100000); if (params.getT() != null && params.getT().isSearch()) { MDC.put("SEARCH", String.valueOf(searchRequestId)); } NewznabResponse.SearchType searchType = getSearchType(); logger.info("Received external {} API call: {}", searchType.name().toLowerCase(), params); if (!noApiKeyNeeded && !Objects.equals(params.getApikey(), configProvider.getBaseConfig().getMain().getApiKey())) { logger.error("Received API call with wrong API key"); throw new WrongApiKeyException("Wrong api key"); } if (!params.getIndexers().isEmpty() && indexerName != null) { logger.error("Received call with parameters set in path and request variables"); NewznabXmlError error = new NewznabXmlError("200", "Received call with parameters set in path and request variables"); return new ResponseEntity<Object>(error, HttpStatus.OK); } else if (indexerName != null) { params.setIndexers(Sets.newHashSet(indexerName)); } if (params.getT() == ActionAttribute.CAPS) { return capsGenerator.getCaps(params.getO(), searchType); } if (Stream.of(ActionAttribute.SEARCH, ActionAttribute.BOOK, ActionAttribute.TVSEARCH, ActionAttribute.MOVIE).anyMatch(x -> x == params.getT())) { if (inMockingMode) { logger.debug("Will mock results for this request"); return new ResponseEntity<>(mockSearch.mockSearch(params, getSearchType() == NewznabResponse.SearchType.NEWZNAB), HttpStatus.OK); } if (params.getCachetime() != null || configProvider.getBaseConfig().getSearching().getGlobalCacheTimeMinutes().isPresent()) { return handleCachingSearch(params, searchType, searchRequestId); } NewznabResponse searchResult = search(params, searchRequestId); HttpHeaders httpHeaders = setSearchTypeAndGetHeaders(params, searchResult); return new ResponseEntity<>(searchResult, httpHeaders, HttpStatus.OK); } if (params.getT() == ActionAttribute.GET) { return getNzb(params); } logger.error("Incorrect API request: {}", params); NewznabXmlError error = new NewznabXmlError("200", "Unknown or incorrect parameter"); return new ResponseEntity<Object>(error, HttpStatus.OK); } }
ExternalApi { @RequestMapping(value = {"/api", "/rss", "/torznab/api", "/torznab/api/{indexerName}", "/api/{indexerName}"}, consumes = MediaType.ALL_VALUE) public ResponseEntity<? extends Object> api(NewznabParameters params, @PathVariable(value = "indexerName", required = false) String indexerName, @PathVariable(value = "mock", required = false) String mock) throws Exception { int searchRequestId = random.nextInt(100000); if (params.getT() != null && params.getT().isSearch()) { MDC.put("SEARCH", String.valueOf(searchRequestId)); } NewznabResponse.SearchType searchType = getSearchType(); logger.info("Received external {} API call: {}", searchType.name().toLowerCase(), params); if (!noApiKeyNeeded && !Objects.equals(params.getApikey(), configProvider.getBaseConfig().getMain().getApiKey())) { logger.error("Received API call with wrong API key"); throw new WrongApiKeyException("Wrong api key"); } if (!params.getIndexers().isEmpty() && indexerName != null) { logger.error("Received call with parameters set in path and request variables"); NewznabXmlError error = new NewznabXmlError("200", "Received call with parameters set in path and request variables"); return new ResponseEntity<Object>(error, HttpStatus.OK); } else if (indexerName != null) { params.setIndexers(Sets.newHashSet(indexerName)); } if (params.getT() == ActionAttribute.CAPS) { return capsGenerator.getCaps(params.getO(), searchType); } if (Stream.of(ActionAttribute.SEARCH, ActionAttribute.BOOK, ActionAttribute.TVSEARCH, ActionAttribute.MOVIE).anyMatch(x -> x == params.getT())) { if (inMockingMode) { logger.debug("Will mock results for this request"); return new ResponseEntity<>(mockSearch.mockSearch(params, getSearchType() == NewznabResponse.SearchType.NEWZNAB), HttpStatus.OK); } if (params.getCachetime() != null || configProvider.getBaseConfig().getSearching().getGlobalCacheTimeMinutes().isPresent()) { return handleCachingSearch(params, searchType, searchRequestId); } NewznabResponse searchResult = search(params, searchRequestId); HttpHeaders httpHeaders = setSearchTypeAndGetHeaders(params, searchResult); return new ResponseEntity<>(searchResult, httpHeaders, HttpStatus.OK); } if (params.getT() == ActionAttribute.GET) { return getNzb(params); } logger.error("Incorrect API request: {}", params); NewznabXmlError error = new NewznabXmlError("200", "Unknown or incorrect parameter"); return new ResponseEntity<Object>(error, HttpStatus.OK); }  }
ExternalApi { @RequestMapping(value = {"/api", "/rss", "/torznab/api", "/torznab/api/{indexerName}", "/api/{indexerName}"}, consumes = MediaType.ALL_VALUE) public ResponseEntity<? extends Object> api(NewznabParameters params, @PathVariable(value = "indexerName", required = false) String indexerName, @PathVariable(value = "mock", required = false) String mock) throws Exception { int searchRequestId = random.nextInt(100000); if (params.getT() != null && params.getT().isSearch()) { MDC.put("SEARCH", String.valueOf(searchRequestId)); } NewznabResponse.SearchType searchType = getSearchType(); logger.info("Received external {} API call: {}", searchType.name().toLowerCase(), params); if (!noApiKeyNeeded && !Objects.equals(params.getApikey(), configProvider.getBaseConfig().getMain().getApiKey())) { logger.error("Received API call with wrong API key"); throw new WrongApiKeyException("Wrong api key"); } if (!params.getIndexers().isEmpty() && indexerName != null) { logger.error("Received call with parameters set in path and request variables"); NewznabXmlError error = new NewznabXmlError("200", "Received call with parameters set in path and request variables"); return new ResponseEntity<Object>(error, HttpStatus.OK); } else if (indexerName != null) { params.setIndexers(Sets.newHashSet(indexerName)); } if (params.getT() == ActionAttribute.CAPS) { return capsGenerator.getCaps(params.getO(), searchType); } if (Stream.of(ActionAttribute.SEARCH, ActionAttribute.BOOK, ActionAttribute.TVSEARCH, ActionAttribute.MOVIE).anyMatch(x -> x == params.getT())) { if (inMockingMode) { logger.debug("Will mock results for this request"); return new ResponseEntity<>(mockSearch.mockSearch(params, getSearchType() == NewznabResponse.SearchType.NEWZNAB), HttpStatus.OK); } if (params.getCachetime() != null || configProvider.getBaseConfig().getSearching().getGlobalCacheTimeMinutes().isPresent()) { return handleCachingSearch(params, searchType, searchRequestId); } NewznabResponse searchResult = search(params, searchRequestId); HttpHeaders httpHeaders = setSearchTypeAndGetHeaders(params, searchResult); return new ResponseEntity<>(searchResult, httpHeaders, HttpStatus.OK); } if (params.getT() == ActionAttribute.GET) { return getNzb(params); } logger.error("Incorrect API request: {}", params); NewznabXmlError error = new NewznabXmlError("200", "Unknown or incorrect parameter"); return new ResponseEntity<Object>(error, HttpStatus.OK); }  @RequestMapping(value = {"/api", "/rss", "/torznab/api", "/torznab/api/{indexerName}", "/api/{indexerName}"}, consumes = MediaType.ALL_VALUE) ResponseEntity<? extends Object> api(NewznabParameters params, @PathVariable(value = "indexerName", required = false) String indexerName, @PathVariable(value = "mock", required = false) String mock); static void setInMockingMode(boolean newValue); @ExceptionHandler(value = ExternalApiException.class) NewznabXmlError handler(ExternalApiException e); @ExceptionHandler(value = Exception.class) ResponseEntity handleUnexpectedError(Exception e); }
ExternalApi { @RequestMapping(value = {"/api", "/rss", "/torznab/api", "/torznab/api/{indexerName}", "/api/{indexerName}"}, consumes = MediaType.ALL_VALUE) public ResponseEntity<? extends Object> api(NewznabParameters params, @PathVariable(value = "indexerName", required = false) String indexerName, @PathVariable(value = "mock", required = false) String mock) throws Exception { int searchRequestId = random.nextInt(100000); if (params.getT() != null && params.getT().isSearch()) { MDC.put("SEARCH", String.valueOf(searchRequestId)); } NewznabResponse.SearchType searchType = getSearchType(); logger.info("Received external {} API call: {}", searchType.name().toLowerCase(), params); if (!noApiKeyNeeded && !Objects.equals(params.getApikey(), configProvider.getBaseConfig().getMain().getApiKey())) { logger.error("Received API call with wrong API key"); throw new WrongApiKeyException("Wrong api key"); } if (!params.getIndexers().isEmpty() && indexerName != null) { logger.error("Received call with parameters set in path and request variables"); NewznabXmlError error = new NewznabXmlError("200", "Received call with parameters set in path and request variables"); return new ResponseEntity<Object>(error, HttpStatus.OK); } else if (indexerName != null) { params.setIndexers(Sets.newHashSet(indexerName)); } if (params.getT() == ActionAttribute.CAPS) { return capsGenerator.getCaps(params.getO(), searchType); } if (Stream.of(ActionAttribute.SEARCH, ActionAttribute.BOOK, ActionAttribute.TVSEARCH, ActionAttribute.MOVIE).anyMatch(x -> x == params.getT())) { if (inMockingMode) { logger.debug("Will mock results for this request"); return new ResponseEntity<>(mockSearch.mockSearch(params, getSearchType() == NewznabResponse.SearchType.NEWZNAB), HttpStatus.OK); } if (params.getCachetime() != null || configProvider.getBaseConfig().getSearching().getGlobalCacheTimeMinutes().isPresent()) { return handleCachingSearch(params, searchType, searchRequestId); } NewznabResponse searchResult = search(params, searchRequestId); HttpHeaders httpHeaders = setSearchTypeAndGetHeaders(params, searchResult); return new ResponseEntity<>(searchResult, httpHeaders, HttpStatus.OK); } if (params.getT() == ActionAttribute.GET) { return getNzb(params); } logger.error("Incorrect API request: {}", params); NewznabXmlError error = new NewznabXmlError("200", "Unknown or incorrect parameter"); return new ResponseEntity<Object>(error, HttpStatus.OK); }  @RequestMapping(value = {"/api", "/rss", "/torznab/api", "/torznab/api/{indexerName}", "/api/{indexerName}"}, consumes = MediaType.ALL_VALUE) ResponseEntity<? extends Object> api(NewznabParameters params, @PathVariable(value = "indexerName", required = false) String indexerName, @PathVariable(value = "mock", required = false) String mock); static void setInMockingMode(boolean newValue); @ExceptionHandler(value = ExternalApiException.class) NewznabXmlError handler(ExternalApiException e); @ExceptionHandler(value = Exception.class) ResponseEntity handleUnexpectedError(Exception e);  }