io.continual.http.app.servers.routeInstallers.BaseRouteInstaller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of continualHttp Show documentation
Show all versions of continualHttp Show documentation
Continual's HTTP service library.
/*
* Copyright 2019, Continual.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.continual.http.app.servers.routeInstallers;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import org.json.JSONObject;
import org.slf4j.Logger;
import io.continual.builder.Builder.BuildFailure;
import io.continual.http.app.servers.CorsOptionsRouter;
import io.continual.http.service.framework.CHttpErrorHandler;
import io.continual.http.service.framework.CHttpRouteInstaller;
import io.continual.http.service.framework.context.CHttpRequestContext;
import io.continual.http.service.framework.routing.CHttpRequestRouter;
import io.continual.http.service.framework.routing.CHttpRouteSource;
import io.continual.http.service.framework.routing.playish.CHttpPlayishInstanceCallRoutingSource;
import io.continual.http.service.framework.routing.playish.CHttpPlayishRoutingFileSource;
import io.continual.resources.ResourceLoader;
import io.continual.util.nv.NvReadable;
import io.continual.util.standards.MimeTypes;
public class BaseRouteInstaller implements CHttpRouteInstaller
{
public BaseRouteInstaller ()
{
this ( true );
}
public BaseRouteInstaller ( boolean withCors )
{
fWithCors = withCors;
fRouteEntries = new LinkedList<> ();
fErrorHandlerEntries = new LinkedList<> ();
}
public BaseRouteInstaller registerRouteSource ( CHttpRouteSource routeSource )
{
fRouteEntries.add ( routeSource );
return this;
}
@Override
public void setupRouter ( CHttpRequestRouter rr, NvReadable prefs ) throws IOException
{
if ( fWithCors )
{
setupCorsHandler ( rr );
}
setupExceptionHandlers ( rr );
for ( CHttpRouteSource rs : fRouteEntries )
{
rr.addRouteSource ( rs );
}
}
public BaseRouteInstaller registerRoutes ( String routeFile, Object handler ) throws BuildFailure
{
return registerRoutes ( routeFile, this.getClass (), handler );
}
public BaseRouteInstaller registerRoutes ( String routeFile, Class> clazz, Object handler ) throws BuildFailure
{
try (
final InputStream is = new ResourceLoader ()
.usingStandardSources ( false, clazz )
.named ( routeFile )
.load ()
;
)
{
if ( is == null )
{
throw new BuildFailure ( "Unable to load " + routeFile + "." );
}
registerRoutes ( is, handler );
}
catch ( IOException x )
{
throw new BuildFailure ( x );
}
return this;
}
public BaseRouteInstaller registerRoutes ( InputStream routeFile, Object handler ) throws BuildFailure
{
if ( routeFile == null )
{
throw new BuildFailure ( "Received a null input stream for routes." );
}
try
{
registerRouteSource ( new CHttpPlayishInstanceCallRoutingSource