Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.ui.impl; |
| 17 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 18 | import org.glassfish.jersey.media.multipart.FormDataParam; |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 19 | import org.onlab.rest.BaseResource; |
| 20 | import org.onosproject.app.ApplicationAdminService; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 21 | import org.onosproject.core.Application; |
| 22 | import org.onosproject.core.ApplicationId; |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 23 | |
| 24 | import javax.ws.rs.Consumes; |
Thomas Vachuska | a42ce0b | 2016-03-09 09:02:49 -0800 | [diff] [blame] | 25 | import javax.ws.rs.DefaultValue; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 26 | import javax.ws.rs.GET; |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 27 | import javax.ws.rs.POST; |
| 28 | import javax.ws.rs.Path; |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 29 | import javax.ws.rs.PathParam; |
| 30 | import javax.ws.rs.Produces; |
Thomas Vachuska | a42ce0b | 2016-03-09 09:02:49 -0800 | [diff] [blame] | 31 | import javax.ws.rs.QueryParam; |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
Thomas Vachuska | a42ce0b | 2016-03-09 09:02:49 -0800 | [diff] [blame] | 36 | import java.util.Objects; |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Application upload resource. |
| 40 | */ |
| 41 | @Path("applications") |
| 42 | public class ApplicationResource extends BaseResource { |
| 43 | |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 44 | private static String lastInstalledAppName = null; |
| 45 | private static final Object LAST_INSTALLED_APP_NAME_LOCK = new Object(); |
Thomas Vachuska | a42ce0b | 2016-03-09 09:02:49 -0800 | [diff] [blame] | 46 | |
| 47 | |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 48 | @Path("upload") |
| 49 | @POST |
| 50 | @Consumes(MediaType.MULTIPART_FORM_DATA) |
Thomas Vachuska | a42ce0b | 2016-03-09 09:02:49 -0800 | [diff] [blame] | 51 | public Response upload(@QueryParam("activate") @DefaultValue("false") String activate, |
| 52 | @FormDataParam("file") InputStream stream) throws IOException { |
| 53 | ApplicationAdminService service = get(ApplicationAdminService.class); |
Sean Condon | 2aa8609 | 2018-07-16 09:04:05 +0100 | [diff] [blame] | 54 | try { |
| 55 | Application app = service.install(stream); |
| 56 | synchronized (LAST_INSTALLED_APP_NAME_LOCK) { |
| 57 | lastInstalledAppName = app.id().name(); |
| 58 | } |
| 59 | if (Objects.equals(activate, "true")) { |
| 60 | service.activate(app.id()); |
| 61 | } |
| 62 | return Response.ok().build(); |
| 63 | } catch (IllegalStateException e) { |
| 64 | return Response.status( |
| 65 | Response.Status.PRECONDITION_FAILED.getStatusCode(), |
| 66 | e.getMessage()).build(); |
| 67 | } catch (Exception e) { |
| 68 | throw e; |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 69 | } |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Thomas Vachuska | 08b4dec | 2017-08-31 15:20:17 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Get application OAR/JAR file. |
| 74 | * Returns the OAR/JAR file used to install the specified application. |
| 75 | * |
| 76 | * @param name application name |
| 77 | * @return 200 OK; 404; 401 |
| 78 | */ |
| 79 | @GET |
| 80 | @Produces(MediaType.APPLICATION_OCTET_STREAM) |
| 81 | @Path("{name}/download") |
| 82 | public Response download(@PathParam("name") String name) { |
| 83 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 84 | ApplicationId appId = service.getId(name); |
| 85 | InputStream bits = service.getApplicationArchive(appId); |
| 86 | String fileName = appId.name() + ".oar"; |
| 87 | return Response.ok(bits) |
| 88 | .header("Content-Disposition", "attachment; filename=\"" + fileName + "\"") |
| 89 | .build(); |
| 90 | } |
| 91 | |
Jian Li | 97d6b2d | 2016-01-20 10:13:43 -0800 | [diff] [blame] | 92 | @Path("{name}/icon") |
| 93 | @GET |
| 94 | @Produces("image/png") |
| 95 | public Response getIcon(@PathParam("name") String name) throws IOException { |
| 96 | ApplicationAdminService service = get(ApplicationAdminService.class); |
| 97 | ApplicationId appId = service.getId(name); |
| 98 | Application app = service.getApplication(appId); |
| 99 | return Response.ok(app.icon()).build(); |
| 100 | } |
Ray Milkey | 06297ed | 2018-01-22 17:13:41 -0800 | [diff] [blame] | 101 | |
| 102 | static String getLastInstalledAppName() { |
| 103 | synchronized (LAST_INSTALLED_APP_NAME_LOCK) { |
| 104 | return lastInstalledAppName; |
| 105 | } |
| 106 | } |
Thomas Vachuska | 530e52a | 2015-05-06 19:51:32 -0700 | [diff] [blame] | 107 | } |