Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting ApplicationContextPath with 0.8 doesn't work #957

Closed
shivangshah opened this issue Mar 28, 2015 · 7 comments
Closed

Getting ApplicationContextPath with 0.8 doesn't work #957

shivangshah opened this issue Mar 28, 2015 · 7 comments

Comments

@shivangshah
Copy link

Hi,

I just upgraded to 0.8 of dropwizard and it seems like the getting of applicationContextPath doesn't work.

System.out.println("CONTEXT PATH: " +environment.getApplicationContext().getContextPath());

Output:

CONTEXT PATH: /

my yml configuration:

server:
    type: context
    maxThreads: 1024
    minThreads: 8
    maxQueuedRequests: 1024
    adminMinThreads: 1
    adminMaxThreads: 64
    applicationContextPath: /mdm/*
    adminContextPath: /admin

Any inputs on this? Is this actually a bug or something changed in 0.8 ?

@arteam
Copy link
Member

arteam commented Mar 29, 2015

Thanks for the report. A couple of remarks and questions:

  1. I believe, there is no reason to add /* to applicationContextPath. It should be just /mdm.
  2. Maybe it's a typo, but type: context doesn't exist. It should be type: simple.
  3. Where do you call environment.getApplicationContext().getContextPath()? For example, in the method Application#run the context path is not set yet.

Please, provide a code sample, where we can inspect a wrong behavior in 0.8.0.

@shivangshah
Copy link
Author

So I made the changes you told me for 1) and 2). Seems like changing 2) actually broke the application altogether and now the server itself doesn't start.

Here's the error

Scanning for projects...

Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1

------------------------------------------------------------------------
Building app 1.0
------------------------------------------------------------------------

--- exec-maven-plugin:1.2.1:exec (default-cli) @ app ---
mdm.yml has an error:
  * Unrecognized field at: server.adminMinThreads
    Did you mean?:
      - minThreads
      - maxThreads
      - adminContextPath
      - allowedMethods
      - gid
        [16 more]

------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time: 2.212 s
Finished at: 2015-03-29T02:20:00-08:00
Final Memory: 16M/309M
------------------------------------------------------------------------

And here's the yml

server:
    type: simple
    maxThreads: 1024
    minThreads: 8
    maxQueuedRequests: 1024
    adminMinThreads: 1
    adminMaxThreads: 64
    applicationContextPath: /mdm
    adminContextPath: /admin

I am going to do more digging on why this happens (maybe something lingering from 0.7 upgrade to 0.8)

@shivangshah
Copy link
Author

So apparently the type: simple doesn't work when you want to use some specific types of properties. So getting rid of them finally got the server running but still can't get to context path. Here's the code sample:
YML Snippet

server:
    maxThreads: 1024
    minThreads: 8
    maxQueuedRequests: 1024
    adminMinThreads: 1
    adminMaxThreads: 64
    applicationContextPath: /mdm
    adminContextPath: /admin
    applicationConnectors:
        - type: http
          port: 8080
          bindHost: 0.0.0.0   # only bind to loopback
          headerCacheSize: 512 bytes
          outputBufferSize: 32KiB
          maxRequestHeaderSize: 8KiB
          maxResponseHeaderSize: 8KiB
          inputBufferSize: 8KiB
          idleTimeout: 30 seconds
          minBufferPoolSize: 64 bytes
          bufferPoolIncrement: 1KiB
          maxBufferPoolSize: 64KiB
          acceptorThreads: 1
          selectorThreads: 2
          acceptQueueSize: 1024
          reuseAddress: true
          soLingerTime: 345s
          useServerHeader: false
          useDateHeader: true
          useForwardedHeaders: true
        - type: https
          port: 8443
          keyStorePath: abc.jks
          keyStorePassword: pqr
          keyStoreType: JKS
          validateCerts: false
          validatePeers: false
    adminConnectors:
        - type: http
          port: 8081

Server Start:

import com.bazaarvoice.dropwizard.assets.ConfiguredAssetsBundle;
import com.totvslabs.mdm.app.config.ServerConfiguration;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

public class MdmApplication extends Application<ServerConfiguration> {
    public static ServerConfiguration serverConfiguration;
    @Override
    public void initialize(Bootstrap<ServerConfiguration> bootstrap) {
        bootstrap.addBundle(new ConfiguredAssetsBundle("/web-apps/swagger-ui/", "/swagger-ui/", "index.html", "swagger"));
    }

    @Override
    public void run(ServerConfiguration serverConfiguration, Environment environment) throws Exception {
        MdmApplication.serverConfiguration = serverConfiguration;
        System.out.println("***************************");
        System.out.println("CONTEXT PATH: " +environment.getApplicationContext().getContextPath());
        System.out.println("***************************");
}
public static void main(String[] args) throws Exception {
        new MdmApplication().run(args);
    }
}

Here's the output (emphasis on the context path):

Scanning for projects...

Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1

------------------------------------------------------------------------
Building app 1.0
------------------------------------------------------------------------

--- exec-maven-plugin:1.2.1:exec (default-cli) @ app ---
02:35:17,300 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
02:35:17,300 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
02:35:17,300 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]
02:35:17,300 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Setting up default configuration.
02:35:18,181 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@7c8326a4 - Propagating INFO level on Logger[ROOT] onto the JUL framework
02:35:18,182 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@7c8326a4 - Propagating INFO level on Logger[com.totvslabs.mdm] onto the JUL framework
02:35:18,182 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@7c8326a4 - Propagating WARN level on Logger[com.couchbase] onto the JUL framework
02:35:18,182 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@7c8326a4 - Propagating WARN level on Logger[org.elasticsearch] onto the JUL framework
02:35:18,182 |-INFO in ch.qos.logback.classic.jul.LevelChangePropagator@7c8326a4 - Propagating WARN level on Logger[org.reflections] onto the JUL framework
02:35:18,184 |-INFO in ch.qos.logback.classic.AsyncAppender[async-console-appender] - Attaching appender named [console-appender] to AsyncAppender.
02:35:18,184 |-INFO in ch.qos.logback.classic.AsyncAppender[async-console-appender] - Setting discardingThreshold to 51
02:35:18,191 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use gz compression
02:35:18,192 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern ../logs/mdm-app-%d.log for the active file
02:35:18,194 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern '../logs/mdm-app-%d.log.gz'.
02:35:18,194 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
02:35:18,196 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Sun Mar 29 02:35:18 PDT 2015
02:35:18,198 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[file-appender] - File property must be set before any triggeringPolicy or rollingPolicy properties
02:35:18,198 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[file-appender] - Visit http://logback.qos.ch/codes.html#rfa_file_after for more information
02:35:18,198 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file-appender] - Active log file name: ../logs/mdm-app.log
02:35:18,198 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[file-appender] - File property is set to [../logs/mdm-app.log]
02:35:18,199 |-INFO in ch.qos.logback.classic.AsyncAppender[async-file-appender] - Attaching appender named [file-appender] to AsyncAppender.
02:35:18,199 |-INFO in ch.qos.logback.classic.AsyncAppender[async-file-appender] - Setting discardingThreshold to 51

INFO  [2015-03-29 09:35:18,223] org.eclipse.jetty.util.log: Logging initialized @1098ms
WARN  [2015-03-29 09:35:18,320] com.google.common.cache.CacheBuilder: ignoring weigher specified without maximumWeight
***************************
CONTEXT PATH: /
***************************
INFO  [2015-03-29 09:35:18,345] io.dropwizard.server.ServerFactory: Starting MdmApplication
INFO  [2015-03-29 09:35:18,599] io.dropwizard.jetty.HttpsConnectorFactory: Supported protocols: [SSLv2Hello, SSLv3, TLSv1, TLSv1.1, TLSv1.2]
INFO  [2015-03-29 09:35:18,599] io.dropwizard.jetty.HttpsConnectorFactory: Supported cipher suites: [TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, TLS_RSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA, SSL_RSA_WITH_RC4_128_SHA, TLS_ECDH_ECDSA_WITH_RC4_128_SHA, TLS_ECDH_RSA_WITH_RC4_128_SHA, SSL_RSA_WITH_RC4_128_MD5, TLS_EMPTY_RENEGOTIATION_INFO_SCSV, TLS_DH_anon_WITH_AES_128_GCM_SHA256, TLS_DH_anon_WITH_AES_128_CBC_SHA256, TLS_ECDH_anon_WITH_AES_128_CBC_SHA, TLS_DH_anon_WITH_AES_128_CBC_SHA, TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA, SSL_DH_anon_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_anon_WITH_RC4_128_SHA, SSL_DH_anon_WITH_RC4_128_MD5, SSL_RSA_WITH_DES_CBC_SHA, SSL_DHE_RSA_WITH_DES_CBC_SHA, SSL_DHE_DSS_WITH_DES_CBC_SHA, SSL_DH_anon_WITH_DES_CBC_SHA, SSL_RSA_EXPORT_WITH_DES40_CBC_SHA, SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA, SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA, SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA, SSL_RSA_EXPORT_WITH_RC4_40_MD5, SSL_DH_anon_EXPORT_WITH_RC4_40_MD5, TLS_RSA_WITH_NULL_SHA256, TLS_ECDHE_ECDSA_WITH_NULL_SHA, TLS_ECDHE_RSA_WITH_NULL_SHA, SSL_RSA_WITH_NULL_SHA, TLS_ECDH_ECDSA_WITH_NULL_SHA, TLS_ECDH_RSA_WITH_NULL_SHA, TLS_ECDH_anon_WITH_NULL_SHA, SSL_RSA_WITH_NULL_MD5, TLS_KRB5_WITH_3DES_EDE_CBC_SHA, TLS_KRB5_WITH_3DES_EDE_CBC_MD5, TLS_KRB5_WITH_RC4_128_SHA, TLS_KRB5_WITH_RC4_128_MD5, TLS_KRB5_WITH_DES_CBC_SHA, TLS_KRB5_WITH_DES_CBC_MD5, TLS_KRB5_EXPORT_WITH_DES_CBC_40_SHA, TLS_KRB5_EXPORT_WITH_DES_CBC_40_MD5, TLS_KRB5_EXPORT_WITH_RC4_40_SHA, TLS_KRB5_EXPORT_WITH_RC4_40_MD5]
INFO  [2015-03-29 09:35:18,624] org.eclipse.jetty.setuid.SetUIDListener: Opened application@6c008c24{HTTP/1.1}{0.0.0.0:8080}
INFO  [2015-03-29 09:35:18,624] org.eclipse.jetty.setuid.SetUIDListener: Opened application@216e0771{SSL-HTTP/1.1}{0.0.0.0:8443}
INFO  [2015-03-29 09:35:18,624] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@21079a12{HTTP/1.1}{0.0.0.0:8081}
INFO  [2015-03-29 09:35:18,626] org.eclipse.jetty.server.Server: jetty-9.2.9.v20150224
INFO  [2015-03-29 09:35:18,989] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:

    NONE

INFO  [2015-03-29 09:35:18,994] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@bf75b5c{/mdm,null,AVAILABLE}
INFO  [2015-03-29 09:35:18,996] io.dropwizard.setup.AdminEnvironment: tasks = 

    POST    /tasks/log-level (io.dropwizard.servlets.tasks.LogConfigurationTask)
    POST    /tasks/gc (io.dropwizard.servlets.tasks.GarbageCollectionTask)

WARN  [2015-03-29 09:35:18,996] io.dropwizard.setup.AdminEnvironment: 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!    THIS APPLICATION HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER KNOW      !
!     IF IT DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF YOU'RE      !
!    LETTING YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH OF YOUR    !
!         APPLICATION'S DEPENDENCIES WHICH FULLY (BUT LIGHTLY) TESTS IT.       !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INFO  [2015-03-29 09:35:19,000] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@2753316c{/admin,null,AVAILABLE}
INFO  [2015-03-29 09:35:19,006] org.eclipse.jetty.server.ServerConnector: Started application@6c008c24{HTTP/1.1}{0.0.0.0:8080}
INFO  [2015-03-29 09:35:19,009] org.eclipse.jetty.server.ServerConnector: Started application@216e0771{SSL-HTTP/1.1}{0.0.0.0:8443}
INFO  [2015-03-29 09:35:19,010] org.eclipse.jetty.server.ServerConnector: Started admin@21079a12{HTTP/1.1}{0.0.0.0:8081}
INFO  [2015-03-29 09:35:19,010] org.eclipse.jetty.server.Server: Started @1886ms

Hope this helps

@joschi
Copy link
Member

joschi commented Mar 29, 2015

@shivangshah You have to differentiate between servers and connectors. Additionally, the different types of servers (default, simple) provide different options.

As @arteam already described, the application context path in DefaultServerFactory and SimpleServerFactory is only set to the configured value after Application#run() has been executed, so you won't be able to get the configured application context path this way.

@joschi joschi closed this as completed Mar 29, 2015
@shivangshah
Copy link
Author

@joschi I am not sure I understand. All the swagger integration docs with dropwizard per the following link (and many others) show how to set the basePath and they all mention the following http://stackoverflow.com/questions/23301054/how-to-use-swagger-with-dropwizard-0-7-0

environment.jersey().register(new ApiListingResourceJSON());
  environment.jersey().register(new ApiDeclarationProvider());
  environment.jersey().register(new ResourceListingProvider());
  ScannerFactory.setScanner(new DefaultJaxrsScanner());
  ClassReaders.setReader(new DefaultJaxrsApiReader());
  SwaggerConfig config = ConfigFactory.config();
  config.setApiVersion(API_VERSION);
  config.setBasePath(".." + environment.getApplicationContext().getContextPath());

I just need to get the applicationContextPath. And from the code above you can see that I do try to get it after Application#run() .. So where am I doing it wrong?

And seems like it does pickup the type "default" if a type is not defined. So that's fine.

@shivangshah
Copy link
Author

Actually on a second run, I found the answer:

@Override
    public void run(ServerConfiguration serverConfiguration, Environment environment) throws Exception {
DefaultServerFactory factory = (DefaultServerFactory) serverConfiguration.getServerFactory();
System.out.println("CONTEXT PATH: "+factory.getApplicationContextPath());
...

This gives me the applicationContextPath from the yml itself which is exactly what I am looking for. Thanks for the help!

@Madhukar07565
Copy link

logging:
level: INFO
loggers:
"io.dropwizard": INFO
"org.hibernate.SQL":
level: DEBUG
additive: false
appenders:
- type: file
currentLogFilename: /var/log/myapplication-sql.log
archivedLogFilenamePattern: /var/log/myapplication-sql-%d.log.gz
archivedFileCount: 5
appenders:
- type: console

while running the application it is throwing below error:

  • Unrecognized field at: logging.type
    Did you mean?:
    • level
    • loggers
    • appenders

But the above example presents in dropwizard documentation.

Please let me know how to solve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants