Skip to content

Commit

Permalink
Cleanup SonarQube warnings from recent commits
Browse files Browse the repository at this point in the history
* Remove unnecessary initializers
* Fix order of variables/methods to match Java style

Signed-off-by: Kevin Risden <krisden@apache.org>
  • Loading branch information
risdenk committed Jan 11, 2019
1 parent b0fac16 commit 1d19a80
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 176 deletions.
Expand Up @@ -22,13 +22,12 @@

public class SimpleDirectoryService extends DefaultDirectoryService {

public SimpleDirectoryService() throws Exception {
public SimpleDirectoryService() throws LdapException {
super();
}

@Override
protected void showSecurityWarnings() throws LdapException {
protected void showSecurityWarnings() {
// NoOp - This prevents confusing warnings from being output.
}

}
Expand Up @@ -15,7 +15,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.knox.gateway.ha.dispatch;

import org.apache.http.Header;
Expand All @@ -26,40 +25,30 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class AtlasApiTrustedProxyHaDispatch extends DefaultHaDispatch {
public AtlasApiTrustedProxyHaDispatch() {
setServiceRole("ATLAS-API");
}

@Override
protected void executeRequest(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest, HttpServletResponse outboundResponse) throws IOException {
HttpResponse inboundResponse = null;
try {
inboundResponse = executeOutboundRequest(outboundRequest);
int statusCode = inboundResponse.getStatusLine().getStatusCode();
Header originalLocationHeader = inboundResponse.getFirstHeader("Location");

public AtlasApiTrustedProxyHaDispatch() {
setServiceRole("ATLAS-API");
}

@Override
public void init() {
super.init();
}

if ((statusCode == HttpServletResponse.SC_MOVED_TEMPORARILY || statusCode == HttpServletResponse.SC_TEMPORARY_REDIRECT) && originalLocationHeader != null) {
inboundResponse.removeHeaders("Location");
failoverRequest(outboundRequest, inboundRequest, outboundResponse, inboundResponse, new Exception("Atlas HA redirection"));
}

@Override
protected void executeRequest(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest, HttpServletResponse outboundResponse) throws IOException {
HttpResponse inboundResponse = null;
try {
inboundResponse = executeOutboundRequest(outboundRequest);
int statusCode = inboundResponse.getStatusLine().getStatusCode();
Header originalLocationHeader = inboundResponse.getFirstHeader("Location");
writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse);


if ((statusCode == HttpServletResponse.SC_MOVED_TEMPORARILY || statusCode == HttpServletResponse.SC_TEMPORARY_REDIRECT) && originalLocationHeader != null) {
inboundResponse.removeHeaders("Location");
failoverRequest(outboundRequest, inboundRequest, outboundResponse, inboundResponse, new Exception("Atlas HA redirection"));
}

writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse);

} catch (IOException e) {
LOG.errorConnectingToServer(outboundRequest.getURI().toString(), e);
failoverRequest(outboundRequest, inboundRequest, outboundResponse, inboundResponse, e);
}
} catch (IOException e) {
LOG.errorConnectingToServer(outboundRequest.getURI().toString(), e);
failoverRequest(outboundRequest, inboundRequest, outboundResponse, inboundResponse, e);
}

}
}
Expand Up @@ -15,7 +15,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.knox.gateway.ha.dispatch;

import org.apache.http.Header;
Expand All @@ -25,61 +24,46 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class AtlasTrustedProxyHaDispatch extends DefaultHaDispatch {
private static Set<String> REQUEST_EXCLUDE_HEADERS = new HashSet<>();

static {
REQUEST_EXCLUDE_HEADERS.add("Content-Length");
}

public AtlasTrustedProxyHaDispatch() {
setServiceRole("ATLAS");
}

@Override
public void init() {
super.init();
}

public AtlasTrustedProxyHaDispatch() {
setServiceRole("ATLAS");
}

@Override
protected void executeRequest(HttpUriRequest outboundRequest,
HttpServletRequest inboundRequest,
HttpServletResponse outboundResponse) throws IOException {
HttpResponse inboundResponse = null;
try {
inboundResponse = executeOutboundRequest(outboundRequest);
@Override
protected void executeRequest(HttpUriRequest outboundRequest,
HttpServletRequest inboundRequest,
HttpServletResponse outboundResponse) throws IOException {
HttpResponse inboundResponse = null;
try {
inboundResponse = executeOutboundRequest(outboundRequest);

int sc = inboundResponse.getStatusLine().getStatusCode();
if (sc == HttpServletResponse.SC_MOVED_TEMPORARILY || sc == HttpServletResponse.SC_TEMPORARY_REDIRECT) {
if (!isLoginRedirect(inboundResponse.getFirstHeader("Location"))) {
inboundResponse.removeHeaders("Location");
failoverRequest(outboundRequest,
inboundRequest,
outboundResponse,
inboundResponse,
new Exception("Atlas HA redirection"));
}
}
int sc = inboundResponse.getStatusLine().getStatusCode();
if (sc == HttpServletResponse.SC_MOVED_TEMPORARILY || sc == HttpServletResponse.SC_TEMPORARY_REDIRECT) {
if (!isLoginRedirect(inboundResponse.getFirstHeader("Location"))) {
inboundResponse.removeHeaders("Location");
failoverRequest(outboundRequest,
inboundRequest,
outboundResponse,
inboundResponse,
new Exception("Atlas HA redirection"));
}
}

writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse);
writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse);

} catch (IOException e) {
LOG.errorConnectingToServer(outboundRequest.getURI().toString(), e);
failoverRequest(outboundRequest, inboundRequest, outboundResponse, inboundResponse, e);
}
} catch (IOException e) {
LOG.errorConnectingToServer(outboundRequest.getURI().toString(), e);
failoverRequest(outboundRequest, inboundRequest, outboundResponse, inboundResponse, e);
}
}

private boolean isLoginRedirect(Header locationHeader) {
boolean result = false;
if (locationHeader != null) {
String value = locationHeader.getValue();
result = (value.endsWith("login.jsp") || value.contains("originalUrl"));
}
return result;
private boolean isLoginRedirect(Header locationHeader) {
boolean result = false;
if (locationHeader != null) {
String value = locationHeader.getValue();
result = (value.endsWith("login.jsp") || value.contains("originalUrl"));
}

return result;
}
}
Expand Up @@ -51,7 +51,6 @@
import java.util.Enumeration;

public class GatewayServlet implements Servlet, Filter {

public static final String GATEWAY_DESCRIPTOR_LOCATION_DEFAULT = "gateway.xml";
public static final String GATEWAY_DESCRIPTOR_LOCATION_PARAM = "gatewayDescriptorLocation";

Expand Down Expand Up @@ -85,7 +84,7 @@ public synchronized void setFilter( GatewayFilter filter ) throws ServletExcepti
}
this.filter = filter;
if( filter != null && filterConfig != null ) {
((Filter) filter).destroy();
filter.destroy();
}
}

Expand Down Expand Up @@ -283,5 +282,4 @@ public Enumeration<String> getInitParameterNames() {
return config.getInitParameterNames();
}
}

}
Expand Up @@ -75,7 +75,6 @@
* name of the directory containing cluster topologies. This value default to "clusters".
*/
public class GatewayConfigImpl extends Configuration implements GatewayConfig {

private static final String GATEWAY_DEFAULT_TOPOLOGY_NAME_PARAM = "default.app.topology.name";
private static final String GATEWAY_DEFAULT_TOPOLOGY_NAME = null;

Expand Down Expand Up @@ -228,7 +227,9 @@ public class GatewayConfigImpl extends Configuration implements GatewayConfig {
static final String DISPATCH_HOST_WHITELIST = GATEWAY_CONFIG_FILE_PREFIX + ".dispatch.whitelist";
static final String DISPATCH_HOST_WHITELIST_SERVICES = DISPATCH_HOST_WHITELIST + ".services";

private List<String> DEFAULT_GLOBAL_RULES_SERVICES;
private static final List<String> DEFAULT_GLOBAL_RULES_SERVICES = Arrays.asList(
"NAMENODE", "JOBTRACKER", "WEBHDFS", "WEBHCAT",
"OOZIE", "WEBHBASE", "HIVE", "RESOURCEMANAGER");

public GatewayConfigImpl() {
init();
Expand Down Expand Up @@ -300,21 +301,13 @@ private void init() {
for( String fileName : GATEWAY_CONFIG_FILENAMES ) {
lastFileUrl = loadConfig( fileName, lastFileUrl );
}
//set default services list
setDefaultGlobalRulesServices();

initGatewayHomeDir( lastFileUrl );

// log whether the scoping cookies to the gateway.path feature is enabled
log.cookieScopingFeatureEnabled(isCookieScopingToPathEnabled());
}

private void setDefaultGlobalRulesServices() {
DEFAULT_GLOBAL_RULES_SERVICES = Arrays.asList(
"NAMENODE", "JOBTRACKER", "WEBHDFS", "WEBHCAT",
"OOZIE", "WEBHBASE", "HIVE", "RESOURCEMANAGER");
}

private void initGatewayHomeDir( URL lastFileUrl ) {
String home = System.getProperty( GATEWAY_HOME_VAR );
if( home != null ) {
Expand Down
Expand Up @@ -67,18 +67,6 @@
public abstract class DeploymentFactory {
private static final JAXBContext jaxbContext = getJAXBContext();

private static JAXBContext getJAXBContext() {
Map<String,String> properties = new HashMap<>(2);
properties.put( "eclipselink-oxm-xml", "org/apache/knox/gateway/topology/topology_binding-xml.xml");
properties.put( "eclipselink.media-type", "application/xml" );

try {
return JAXBContext.newInstance(Topology.class.getPackage().getName(), Topology.class.getClassLoader(), properties);
} catch (JAXBException e) {
throw new IllegalStateException(e);
}
}

private static final String SERVLET_NAME_SUFFIX = "-knox-gateway-servlet";
private static final String FILTER_NAME_SUFFIX = "-knox-gateway-filter";
private static final GatewayMessages log = MessagesFactory.get( GatewayMessages.class );
Expand All @@ -95,6 +83,18 @@ private static JAXBContext getJAXBContext() {
loadProviderContributors();
}

private static JAXBContext getJAXBContext() {
Map<String,String> properties = new HashMap<>(2);
properties.put( "eclipselink-oxm-xml", "org/apache/knox/gateway/topology/topology_binding-xml.xml");
properties.put( "eclipselink.media-type", "application/xml" );

try {
return JAXBContext.newInstance(Topology.class.getPackage().getName(), Topology.class.getClassLoader(), properties);
} catch (JAXBException e) {
throw new IllegalStateException(e);
}
}

public static void setGatewayServices(GatewayServices services) {
DeploymentFactory.gatewayServices = services;
}
Expand Down
Expand Up @@ -58,14 +58,6 @@
public class ApplicationDeploymentContributor extends ServiceDeploymentContributorBase {
private static final JAXBContext jaxbContext = getJAXBContext();

private static JAXBContext getJAXBContext() {
try {
return JAXBContext.newInstance( ServiceDefinition.class );
} catch (JAXBException e) {
throw new IllegalStateException(e);
}
}

private static final String SERVICE_DEFINITION_FILE_NAME = "service.xml";
private static final String REWRITE_RULES_FILE_NAME = "rewrite.xml";
private static final String XFORWARDED_FILTER_NAME = "XForwardedHeaderFilter";
Expand All @@ -77,6 +69,14 @@ private static JAXBContext getJAXBContext() {

private UrlRewriteRulesDescriptor serviceRules;

private static JAXBContext getJAXBContext() {
try {
return JAXBContext.newInstance( ServiceDefinition.class );
} catch (JAXBException e) {
throw new IllegalStateException(e);
}
}

private static ServiceDefinition loadServiceDefinition( Application application, File file ) throws JAXBException, FileNotFoundException, IOException {
ServiceDefinition definition;
if( !file.exists() ) {
Expand Down
Expand Up @@ -90,19 +90,6 @@ public class DefaultTopologyService

private static final JAXBContext jaxbContext = getJAXBContext();

private static JAXBContext getJAXBContext() {
String pkgName = Topology.class.getPackage().getName();
String bindingFile = pkgName.replace(".", "/") + "/topology_binding-xml.xml";

Map<String, Object> properties = new HashMap<>(1);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindingFile);
try {
return JAXBContext.newInstance(pkgName, Topology.class.getClassLoader(), properties);
} catch (JAXBException e) {
throw new IllegalStateException(e);
}
}

private static Auditor auditor = AuditServiceFactory.getAuditService().getAuditor(
AuditConstants.DEFAULT_AUDITOR_NAME, AuditConstants.KNOX_SERVICE_NAME,
AuditConstants.KNOX_COMPONENT_NAME);
Expand Down Expand Up @@ -130,6 +117,19 @@ private static JAXBContext getJAXBContext() {

private GatewayConfig config;

private static JAXBContext getJAXBContext() {
String pkgName = Topology.class.getPackage().getName();
String bindingFile = pkgName.replace(".", "/") + "/topology_binding-xml.xml";

Map<String, Object> properties = new HashMap<>(1);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, bindingFile);
try {
return JAXBContext.newInstance(pkgName, Topology.class.getClassLoader(), properties);
} catch (JAXBException e) {
throw new IllegalStateException(e);
}
}

private Topology loadTopology(File file) throws IOException, SAXException, URISyntaxException, InterruptedException {
final long TIMEOUT = 250; //ms
final long DELAY = 50; //ms
Expand Down
Expand Up @@ -40,24 +40,22 @@
import java.util.Map;

public class ProviderConfigurationParser {

private static final JAXBContext jaxbContext = getJAXBContext();

private static JAXBContext getJAXBContext() {
try {
return JAXBContext.newInstance(XMLProviderConfiguration.class);
} catch (JAXBException e) {
throw new IllegalStateException(e);
}
}

private static final String EXT_XML = "xml";
private static final String EXT_JSON = "json";
private static final String EXT_YML = "yml";
private static final String EXT_YAML = "yaml";

public static final List<String> SUPPORTED_EXTENSIONS = Collections.unmodifiableList(Arrays.asList(EXT_XML, EXT_JSON, EXT_YML, EXT_YAML));

private static JAXBContext getJAXBContext() {
try {
return JAXBContext.newInstance(XMLProviderConfiguration.class);
} catch (JAXBException e) {
throw new IllegalStateException(e);
}
}

public static ProviderConfiguration parse(String path) throws Exception {
return parse(new File(path));
Expand Down

0 comments on commit 1d19a80

Please sign in to comment.