@Test public void testInvalidRequest() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setRequestURI( "/feeds?invalid_param=xxx" ); request.addHeader( "User-Agent", "Apache Archiva unit test" ); request.setMethod( "GET" ); MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse(); rssFeedServlet.doGet( request, mockHttpServletResponse ); assertEquals( HttpServletResponse.SC_BAD_REQUEST, mockHttpServletResponse.getStatus() ); }
@Override public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { String repoId = null; String groupId = null; String artifactId = null; String url = StringUtils.removeEnd( req.getRequestURL().toString(), "/" ); if ( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) > 0 ) { artifactId = StringUtils.substringAfterLast( url, "/" ); groupId = StringUtils.substringBeforeLast( StringUtils.substringAfter( url, "feeds/" ), "/" ); groupId = StringUtils.replaceChars( groupId, '/', '.' ); } else if ( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) == 0 ) { if ( StringUtils.countMatches( url, "feeds?" ) > 0 ) { res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." ); return; } repoId = StringUtils.substringAfterLast( url, "/" ); } else { res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." ); return; } RssFeedProcessor processor = null; try { Map<String, String> map = new HashMap<>(); SyndFeed feed = null; if ( isAllowed( req, repoId, groupId, artifactId ) ) { if ( repoId != null ) { processor = newArtifactsprocessor; map.put( RssFeedProcessor.KEY_REPO_ID, repoId ); } else if ( ( groupId != null ) && ( artifactId != null ) ) { processor = newVersionsprocessor; map.put( RssFeedProcessor.KEY_GROUP_ID, groupId ); map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId ); } } else { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED ); return; } feed = processor.process( map ); if ( feed == null ) { res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." ); return; } res.setContentType( MIME_TYPE ); if ( repoId != null ) { feed.setLink( req.getRequestURL().toString() ); } else if ( ( groupId != null ) && ( artifactId != null ) ) { feed.setLink( req.getRequestURL().toString() ); } SyndFeedOutput output = new SyndFeedOutput(); output.output( feed, res.getWriter() ); } catch ( UserNotFoundException unfe ) { log.debug( COULD_NOT_AUTHENTICATE_USER, unfe ); res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( AccountLockedException acce ) { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( AuthenticationException authe ) { log.debug( COULD_NOT_AUTHENTICATE_USER, authe ); res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( FeedException ex ) { log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex ); res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR ); } catch ( MustChangePasswordException e ) { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( UnauthorizedException e ) { log.debug( e.getMessage() ); if ( repoId != null ) { res.setHeader( "WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" ); } else { res.setHeader( "WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId ); } res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED ); } }
RssFeedServlet extends HttpServlet { @Override public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { String repoId = null; String groupId = null; String artifactId = null; String url = StringUtils.removeEnd( req.getRequestURL().toString(), "/" ); if ( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) > 0 ) { artifactId = StringUtils.substringAfterLast( url, "/" ); groupId = StringUtils.substringBeforeLast( StringUtils.substringAfter( url, "feeds/" ), "/" ); groupId = StringUtils.replaceChars( groupId, '/', '.' ); } else if ( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) == 0 ) { if ( StringUtils.countMatches( url, "feeds?" ) > 0 ) { res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." ); return; } repoId = StringUtils.substringAfterLast( url, "/" ); } else { res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." ); return; } RssFeedProcessor processor = null; try { Map<String, String> map = new HashMap<>(); SyndFeed feed = null; if ( isAllowed( req, repoId, groupId, artifactId ) ) { if ( repoId != null ) { processor = newArtifactsprocessor; map.put( RssFeedProcessor.KEY_REPO_ID, repoId ); } else if ( ( groupId != null ) && ( artifactId != null ) ) { processor = newVersionsprocessor; map.put( RssFeedProcessor.KEY_GROUP_ID, groupId ); map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId ); } } else { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED ); return; } feed = processor.process( map ); if ( feed == null ) { res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." ); return; } res.setContentType( MIME_TYPE ); if ( repoId != null ) { feed.setLink( req.getRequestURL().toString() ); } else if ( ( groupId != null ) && ( artifactId != null ) ) { feed.setLink( req.getRequestURL().toString() ); } SyndFeedOutput output = new SyndFeedOutput(); output.output( feed, res.getWriter() ); } catch ( UserNotFoundException unfe ) { log.debug( COULD_NOT_AUTHENTICATE_USER, unfe ); res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( AccountLockedException acce ) { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( AuthenticationException authe ) { log.debug( COULD_NOT_AUTHENTICATE_USER, authe ); res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( FeedException ex ) { log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex ); res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR ); } catch ( MustChangePasswordException e ) { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( UnauthorizedException e ) { log.debug( e.getMessage() ); if ( repoId != null ) { res.setHeader( "WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" ); } else { res.setHeader( "WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId ); } res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED ); } } }
RssFeedServlet extends HttpServlet { @Override public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { String repoId = null; String groupId = null; String artifactId = null; String url = StringUtils.removeEnd( req.getRequestURL().toString(), "/" ); if ( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) > 0 ) { artifactId = StringUtils.substringAfterLast( url, "/" ); groupId = StringUtils.substringBeforeLast( StringUtils.substringAfter( url, "feeds/" ), "/" ); groupId = StringUtils.replaceChars( groupId, '/', '.' ); } else if ( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) == 0 ) { if ( StringUtils.countMatches( url, "feeds?" ) > 0 ) { res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." ); return; } repoId = StringUtils.substringAfterLast( url, "/" ); } else { res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." ); return; } RssFeedProcessor processor = null; try { Map<String, String> map = new HashMap<>(); SyndFeed feed = null; if ( isAllowed( req, repoId, groupId, artifactId ) ) { if ( repoId != null ) { processor = newArtifactsprocessor; map.put( RssFeedProcessor.KEY_REPO_ID, repoId ); } else if ( ( groupId != null ) && ( artifactId != null ) ) { processor = newVersionsprocessor; map.put( RssFeedProcessor.KEY_GROUP_ID, groupId ); map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId ); } } else { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED ); return; } feed = processor.process( map ); if ( feed == null ) { res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." ); return; } res.setContentType( MIME_TYPE ); if ( repoId != null ) { feed.setLink( req.getRequestURL().toString() ); } else if ( ( groupId != null ) && ( artifactId != null ) ) { feed.setLink( req.getRequestURL().toString() ); } SyndFeedOutput output = new SyndFeedOutput(); output.output( feed, res.getWriter() ); } catch ( UserNotFoundException unfe ) { log.debug( COULD_NOT_AUTHENTICATE_USER, unfe ); res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( AccountLockedException acce ) { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( AuthenticationException authe ) { log.debug( COULD_NOT_AUTHENTICATE_USER, authe ); res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( FeedException ex ) { log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex ); res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR ); } catch ( MustChangePasswordException e ) { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( UnauthorizedException e ) { log.debug( e.getMessage() ); if ( repoId != null ) { res.setHeader( "WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" ); } else { res.setHeader( "WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId ); } res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED ); } }  }
RssFeedServlet extends HttpServlet { @Override public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { String repoId = null; String groupId = null; String artifactId = null; String url = StringUtils.removeEnd( req.getRequestURL().toString(), "/" ); if ( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) > 0 ) { artifactId = StringUtils.substringAfterLast( url, "/" ); groupId = StringUtils.substringBeforeLast( StringUtils.substringAfter( url, "feeds/" ), "/" ); groupId = StringUtils.replaceChars( groupId, '/', '.' ); } else if ( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) == 0 ) { if ( StringUtils.countMatches( url, "feeds?" ) > 0 ) { res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." ); return; } repoId = StringUtils.substringAfterLast( url, "/" ); } else { res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." ); return; } RssFeedProcessor processor = null; try { Map<String, String> map = new HashMap<>(); SyndFeed feed = null; if ( isAllowed( req, repoId, groupId, artifactId ) ) { if ( repoId != null ) { processor = newArtifactsprocessor; map.put( RssFeedProcessor.KEY_REPO_ID, repoId ); } else if ( ( groupId != null ) && ( artifactId != null ) ) { processor = newVersionsprocessor; map.put( RssFeedProcessor.KEY_GROUP_ID, groupId ); map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId ); } } else { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED ); return; } feed = processor.process( map ); if ( feed == null ) { res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." ); return; } res.setContentType( MIME_TYPE ); if ( repoId != null ) { feed.setLink( req.getRequestURL().toString() ); } else if ( ( groupId != null ) && ( artifactId != null ) ) { feed.setLink( req.getRequestURL().toString() ); } SyndFeedOutput output = new SyndFeedOutput(); output.output( feed, res.getWriter() ); } catch ( UserNotFoundException unfe ) { log.debug( COULD_NOT_AUTHENTICATE_USER, unfe ); res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( AccountLockedException acce ) { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( AuthenticationException authe ) { log.debug( COULD_NOT_AUTHENTICATE_USER, authe ); res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( FeedException ex ) { log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex ); res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR ); } catch ( MustChangePasswordException e ) { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( UnauthorizedException e ) { log.debug( e.getMessage() ); if ( repoId != null ) { res.setHeader( "WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" ); } else { res.setHeader( "WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId ); } res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED ); } }  @Override void init( ServletConfig servletConfig ); @Override void doGet( HttpServletRequest req, HttpServletResponse res ); }
RssFeedServlet extends HttpServlet { @Override public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { String repoId = null; String groupId = null; String artifactId = null; String url = StringUtils.removeEnd( req.getRequestURL().toString(), "/" ); if ( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) > 0 ) { artifactId = StringUtils.substringAfterLast( url, "/" ); groupId = StringUtils.substringBeforeLast( StringUtils.substringAfter( url, "feeds/" ), "/" ); groupId = StringUtils.replaceChars( groupId, '/', '.' ); } else if ( StringUtils.countMatches( StringUtils.substringAfter( url, "feeds/" ), "/" ) == 0 ) { if ( StringUtils.countMatches( url, "feeds?" ) > 0 ) { res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." ); return; } repoId = StringUtils.substringAfterLast( url, "/" ); } else { res.sendError( HttpServletResponse.SC_BAD_REQUEST, "Invalid request url." ); return; } RssFeedProcessor processor = null; try { Map<String, String> map = new HashMap<>(); SyndFeed feed = null; if ( isAllowed( req, repoId, groupId, artifactId ) ) { if ( repoId != null ) { processor = newArtifactsprocessor; map.put( RssFeedProcessor.KEY_REPO_ID, repoId ); } else if ( ( groupId != null ) && ( artifactId != null ) ) { processor = newVersionsprocessor; map.put( RssFeedProcessor.KEY_GROUP_ID, groupId ); map.put( RssFeedProcessor.KEY_ARTIFACT_ID, artifactId ); } } else { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED ); return; } feed = processor.process( map ); if ( feed == null ) { res.sendError( HttpServletResponse.SC_NO_CONTENT, "No information available." ); return; } res.setContentType( MIME_TYPE ); if ( repoId != null ) { feed.setLink( req.getRequestURL().toString() ); } else if ( ( groupId != null ) && ( artifactId != null ) ) { feed.setLink( req.getRequestURL().toString() ); } SyndFeedOutput output = new SyndFeedOutput(); output.output( feed, res.getWriter() ); } catch ( UserNotFoundException unfe ) { log.debug( COULD_NOT_AUTHENTICATE_USER, unfe ); res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( AccountLockedException acce ) { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( AuthenticationException authe ) { log.debug( COULD_NOT_AUTHENTICATE_USER, authe ); res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( FeedException ex ) { log.debug( COULD_NOT_GENERATE_FEED_ERROR, ex ); res.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, COULD_NOT_GENERATE_FEED_ERROR ); } catch ( MustChangePasswordException e ) { res.sendError( HttpServletResponse.SC_UNAUTHORIZED, COULD_NOT_AUTHENTICATE_USER ); } catch ( UnauthorizedException e ) { log.debug( e.getMessage() ); if ( repoId != null ) { res.setHeader( "WWW-Authenticate", "Basic realm=\"Repository Archiva Managed " + repoId + " Repository" ); } else { res.setHeader( "WWW-Authenticate", "Basic realm=\"Artifact " + groupId + ":" + artifactId ); } res.sendError( HttpServletResponse.SC_UNAUTHORIZED, USER_NOT_AUTHORIZED ); } }  @Override void init( ServletConfig servletConfig ); @Override void doGet( HttpServletRequest req, HttpServletResponse res ); static final String MIME_TYPE; }