Host apple-app-association-file in tomcat web server

1.9k Views Asked by At

We have a requirement of enabling universal link in our application. We have a Spring MVC based web application and a iOS app. To enable universal link as per apple we need to create a json file apple-app-association-file and host this file in the server. Now java web app is deployed in tomcat in windows server and apche 2.4 is being used as web server. Please let me know how to host the apple-app-association-file in the tomcat or web server or inside the war file(inside the code), we are using maven structure. according to docs, we need to remove the file extentsion and file should be access as below:

url of web app: https://xyz.example.com

where xyz.example.com is mapped to a web app which is there in webapp folder in tomcat.(localhost:8080/webApp)

apple-app-association-file to be accessed as: https://xyz.example.com/apple-app-association-file

now as the extension is not there how can i host it.Do i need to make the code changes and treated it as servle request. Even if i do so it wont be a good idea to execute a servet just to access a file

Also, it's also important that the file is served with the correct MIME-type, for Universal Links it can be served as application/json. How to set mime type in tomcat or java web app(spring)

3

There are 3 best solutions below

0
On

I did it with a standard REST controller + endpoint.

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

@RestController
@RequestMapping("/.well-known")
@Slf4j
public class WebClientConfig {

    @GetMapping(value = "/apple-app-site-association",
        produces = MediaType.APPLICATION_JSON_VALUE)
    public String addResourceHandlers() {
        String json = "";
        InputStream inputStream = getClass().getResourceAsStream("/apple-app-association.json");
        try(InputStream stream = inputStream) {
            json = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
        } catch (IOException ioe) {
            log.error("Apple app association could not be retrieved! iOS app will be impacted. Error: " +
                ioe.getMessage());
        }
        return json;
    }
}

Note: the apple-app-asociation.json file is under src/main/resources

0
On

The Solution from pITer Simonov works for me! But i had to add the root path inside

< servlet-mapping > (in web.xml)

like this:

< url-pattern >/</url-pattern >

After that, the resource handler work fine!

0
On

First rename file to apple-app-site-association.json, then write next Spring configuration:

@EnableWebMvc
public class WebClientConfig extends WebMvcConfigurerAdapter {

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/.well-known/*")
      .addResourceLocations("/path/to/your/static/resources")
      .resourceChain(true)
      .addResolver(new PathResourceResolver() {
        @Override
        protected Resource getResource(String resourcePath, Resource location) throws IOException {
          if (resourcePath.equals("apple-app-site-association")) {
            return location.createRelative("apple-app-site-association.json");
          }
          return super.getResource(resourcePath, location);
        }
      });

  }
}

As described here: developer.apple.com

You can place the file at the root of your server or in the .well-known subdirectory.

Then the file will be served with the correct MIME-type "application/json" and accessed as: https://xyz.example.com/.well-known/apple-app-association-file