blob: 16f288a281d087288109022d5c7a329a3355a119 [file] [log] [blame]
Thomas Vachuska530e52a2015-05-06 19:51:32 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska530e52a2015-05-06 19:51:32 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.ui.impl;
17
Jian Li9d616492016-03-09 10:52:49 -080018import org.glassfish.jersey.media.multipart.FormDataParam;
Thomas Vachuska530e52a2015-05-06 19:51:32 -070019import org.onlab.rest.BaseResource;
20import org.onosproject.app.ApplicationAdminService;
Jian Li97d6b2d2016-01-20 10:13:43 -080021import org.onosproject.core.Application;
22import org.onosproject.core.ApplicationId;
Thomas Vachuska530e52a2015-05-06 19:51:32 -070023
24import javax.ws.rs.Consumes;
Thomas Vachuskaa42ce0b2016-03-09 09:02:49 -080025import javax.ws.rs.DefaultValue;
Jian Li97d6b2d2016-01-20 10:13:43 -080026import javax.ws.rs.GET;
Thomas Vachuska530e52a2015-05-06 19:51:32 -070027import javax.ws.rs.POST;
28import javax.ws.rs.Path;
Jian Li97d6b2d2016-01-20 10:13:43 -080029import javax.ws.rs.PathParam;
30import javax.ws.rs.Produces;
Thomas Vachuskaa42ce0b2016-03-09 09:02:49 -080031import javax.ws.rs.QueryParam;
Thomas Vachuska530e52a2015-05-06 19:51:32 -070032import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
34import java.io.IOException;
35import java.io.InputStream;
Thomas Vachuskaa42ce0b2016-03-09 09:02:49 -080036import java.util.Objects;
Thomas Vachuska530e52a2015-05-06 19:51:32 -070037
38/**
39 * Application upload resource.
40 */
41@Path("applications")
42public class ApplicationResource extends BaseResource {
43
Thomas Vachuskaa42ce0b2016-03-09 09:02:49 -080044 static String lastInstalledAppName = null;
45
46
Thomas Vachuska530e52a2015-05-06 19:51:32 -070047 @Path("upload")
48 @POST
49 @Consumes(MediaType.MULTIPART_FORM_DATA)
Thomas Vachuskaa42ce0b2016-03-09 09:02:49 -080050 public Response upload(@QueryParam("activate") @DefaultValue("false") String activate,
51 @FormDataParam("file") InputStream stream) throws IOException {
52 ApplicationAdminService service = get(ApplicationAdminService.class);
53 Application app = service.install(stream);
54 lastInstalledAppName = app.id().name();
55 if (Objects.equals(activate, "true")) {
56 service.activate(app.id());
57 }
Bri Prebilic Cole522e7562015-06-22 15:56:25 -070058 return Response.ok().build();
Thomas Vachuska530e52a2015-05-06 19:51:32 -070059 }
60
Thomas Vachuska08b4dec2017-08-31 15:20:17 -070061 /**
62 * Get application OAR/JAR file.
63 * Returns the OAR/JAR file used to install the specified application.
64 *
65 * @param name application name
66 * @return 200 OK; 404; 401
67 */
68 @GET
69 @Produces(MediaType.APPLICATION_OCTET_STREAM)
70 @Path("{name}/download")
71 public Response download(@PathParam("name") String name) {
72 ApplicationAdminService service = get(ApplicationAdminService.class);
73 ApplicationId appId = service.getId(name);
74 InputStream bits = service.getApplicationArchive(appId);
75 String fileName = appId.name() + ".oar";
76 return Response.ok(bits)
77 .header("Content-Disposition", "attachment; filename=\"" + fileName + "\"")
78 .build();
79 }
80
Jian Li97d6b2d2016-01-20 10:13:43 -080081 @Path("{name}/icon")
82 @GET
83 @Produces("image/png")
84 public Response getIcon(@PathParam("name") String name) throws IOException {
85 ApplicationAdminService service = get(ApplicationAdminService.class);
86 ApplicationId appId = service.getId(name);
87 Application app = service.getApplication(appId);
88 return Response.ok(app.icon()).build();
89 }
Thomas Vachuska530e52a2015-05-06 19:51:32 -070090}