• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

WindhoverLabs / yamcs-cfs / #150

05 Mar 2024 07:27PM UTC coverage: 0.0%. First build
#150

push

lorenzo-gomez-windhover
-Archive Beb plugin. WIP.

0 of 49 new or added lines in 3 files covered. (0.0%)

0 of 6900 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/com/windhoverlabs/beb/BebHandler.java
1
package com.windhoverlabs.beb;
2

3
import static io.netty.handler.codec.http.HttpHeaderNames.CACHE_CONTROL;
4
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
5
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
6
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
7
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
8
// import static org.yamcs.web.WebFileDeployer.PATH_INDEX;
9
// import static org.yamcs.web.WebFileDeployer.PATH_NGSW;
10
// import static org.yamcs.web.WebFileDeployer.PATH_WEBMANIFEST;
11

12
import com.google.common.io.ByteStreams;
13
import io.netty.buffer.ByteBuf;
14
import io.netty.buffer.ByteBufOutputStream;
15
import io.netty.handler.codec.http.DefaultFullHttpResponse;
16
import java.io.File;
17
import java.io.IOException;
18
import java.io.InputStream;
19
import java.nio.file.Files;
20
import java.nio.file.NoSuchFileException;
21
import java.nio.file.Path;
22
import java.nio.file.Paths;
23
import java.util.List;
24
import java.util.stream.Collectors;
25
import java.util.stream.Stream;
26
import org.yamcs.YConfiguration;
27
import org.yamcs.http.HandlerContext;
28
import org.yamcs.http.HttpServer;
29
import org.yamcs.http.InternalServerErrorException;
30
import org.yamcs.http.NotFoundException;
31
import org.yamcs.http.StaticFileHandler;
32

33
/**
34
 * Serves the Yamcs UI, which is an Angular web application.
35
 *
36
 * <p>This class mostly serves static files generated by the Angular compiler, but will insert some
37
 * dynamic elements. Specifically:
38
 *
39
 * <ul>
40
 *   <li>Allow hosting on custom context path, without needing a recompile.
41
 *   <li>Serve PWA webmanifest for local installation.
42
 *   <li>Serve index.html with dynamically inserted configuration elements.
43
 *   <li>Serve custom logo, without needing a recompile.
44
 * </ul>
45
 */
46
public class BebHandler extends StaticFileHandler {
47

48
  // Dev only for now
49
  private static boolean PWA = false;
×
50

51
  private Path indexFile;
52

53
  private String logo;
54
  private Path logoFile;
55

56
  public BebHandler(
57
      YConfiguration config,
58
      HttpServer httpServer,
59
      Path mainDirectory,
60
      List<Path> extraStaticRoots) {
61
    super(
×
62
        "",
63
        Stream.concat(Stream.of(mainDirectory), extraStaticRoots.stream())
×
64
            .collect(Collectors.toList()));
×
65

NEW
66
    var staticRoots =
×
NEW
67
        Stream.concat(Stream.of(mainDirectory), extraStaticRoots.stream())
×
NEW
68
            .collect(Collectors.toList());
×
69

NEW
70
    System.out.println("staticRoots:" + staticRoots);
×
NEW
71
    System.out.println("extraStaticRoots:" + extraStaticRoots);
×
NEW
72
    System.out.println("mainDirectory:" + mainDirectory);
×
73
    indexFile = mainDirectory.resolve(com.windhoverlabs.beb.WebFileDeployer.PATH_INDEX);
×
74

75
    if (config.containsKey("logo")) {
×
76
      logoFile = Paths.get(config.getString("logo"));
×
77
      logo = logoFile.getFileName().toString();
×
78
    }
79
  }
×
80

81
  @Override
82
  public void handle(HandlerContext ctx) {
83

NEW
84
    System.out.println("BEB handle***1");
×
85
    String filePath = getFilePath(ctx);
×
86

87
    // Serve custom brand, if configured
88
    if (logo != null && logo.equals(filePath)) {
×
NEW
89
      System.out.println("BEB handle***2");
×
90
      serveLogo(ctx);
×
91
      return;
×
92
    }
93

NEW
94
    System.out.println("BEB handle***3:" + filePath);
×
95
    //    switch (filePath) {
96
    //      case com.windhoverlabs.beb.WebFileDeployer.PATH_WEBMANIFEST:
97
    //        if (!PWA) {
98
    //          System.out.println("BEB handle***4");
99
    //          throw new NotFoundException();
100
    //        }
101
    //        System.out.println("BEB handle***5");
102
    //        serveUncached(ctx, webManifestFile, "application/manifest+json");
103
    //        return;
104
    //      case com.windhoverlabs.beb.WebFileDeployer.PATH_NGSW:
105
    //        if (!PWA) {
106
    //          System.out.println("BEB handle***6");
107
    //          throw new NotFoundException();
108
    //        }
109
    //        System.out.println("BEB handle***7");
110
    //        serveUncached(ctx, ngswFile, "application/json");
111
    //        return;
112
    //    }
113

NEW
114
    System.out.println("BEB handle***4:" + filePath);
×
115
    // Try to serve a static file
116
    File file = locateFile(filePath);
×
117
    //    if (file != null && !filePath.isEmpty()) {
118
    //      System.out.println("BEB handle***5:" + file);
119
    //      super.handle(ctx);
120
    //      return;
121
    //    }
122

123
    // Set-up HTML5 deep-linking:
124
    // Catch any non-handled URL and make it return the contents of our index.html
125
    // This will cause initialization of the Angular app on any requested path. The
126
    // Angular router will interpret this and do client-side routing as needed.
NEW
127
    System.out.println("BEB handle***6:" + file);
×
NEW
128
    serveUncached(ctx, Paths.get(filePath), "application/json");
×
129
    //    serveUncached(ctx, indexFile, "application/json");
130
  }
×
131

132
  /**
133
   * Sends a rendered template, while recommend clients to not cache it. We hash all of our web
134
   * files, and this reduces likelihood of attempting to load the app from an outdated index.html
135
   */
136
  private void serveUncached(HandlerContext ctx, Path file, String contentType) {
137
    ctx.requireGET();
×
138

139
    ByteBuf body = ctx.createByteBuf();
×
140
    try (InputStream fileIn = Files.newInputStream(file);
×
141
        ByteBufOutputStream bufOut = new ByteBufOutputStream(body)) {
×
142
      ByteStreams.copy(fileIn, bufOut);
×
143
    } catch (NoSuchFileException e) {
×
144
      throw new NotFoundException(e);
×
145
    } catch (IOException e) {
×
146
      throw new InternalServerErrorException(e);
×
147
    }
×
148

149
    DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, body);
×
150
    response.headers().set(CONTENT_TYPE, contentType);
×
151
    response.headers().set(CONTENT_LENGTH, body.readableBytes());
×
152
    response.headers().set(CACHE_CONTROL, "no-store, must-revalidate");
×
153
    ctx.sendResponse(response);
×
154
  }
×
155

156
  private void serveLogo(HandlerContext ctx) {
157
    ctx.requireGET();
×
158

159
    ByteBuf body = ctx.createByteBuf();
×
160
    try (InputStream in = Files.newInputStream(logoFile);
×
161
        ByteBufOutputStream out = new ByteBufOutputStream(body)) {
×
162
      ByteStreams.copy(in, out);
×
163
    } catch (NoSuchFileException e) {
×
164
      throw new NotFoundException();
×
165
    } catch (IOException e) {
×
166
      throw new InternalServerErrorException(e);
×
167
    }
×
168

169
    DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, body);
×
170
    response.headers().set(CONTENT_TYPE, MIME.getMimetype(logoFile));
×
171
    response.headers().set(CONTENT_LENGTH, body.readableBytes());
×
172
    response.headers().set(CACHE_CONTROL, "private, max-age=86400");
×
173
    ctx.sendResponse(response);
×
174
  }
×
175
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc