|
| 1 | +/* |
| 2 | + * Copyright 2013, 2014 EnergyOS.org |
| 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 | + |
| 17 | +package org.energyos.espi.thirdparty.web.custodian; |
| 18 | + |
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.energyos.espi.common.domain.ApplicationInformation; |
| 23 | +import org.energyos.espi.common.domain.Authorization; |
| 24 | +import org.energyos.espi.common.domain.RetailCustomer; |
| 25 | +import org.energyos.espi.common.domain.Routes; |
| 26 | +import org.energyos.espi.common.models.atom.EntryType; |
| 27 | +import org.energyos.espi.common.service.ImportService; |
| 28 | +import org.energyos.espi.common.service.ResourceService; |
| 29 | +import org.energyos.espi.common.service.RetailCustomerService; |
| 30 | +import org.energyos.espi.thirdparty.web.BaseController; |
| 31 | +import org.springframework.beans.factory.annotation.Autowired; |
| 32 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 33 | +import org.springframework.http.HttpEntity; |
| 34 | +import org.springframework.http.HttpHeaders; |
| 35 | +import org.springframework.http.HttpMethod; |
| 36 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 37 | +import org.springframework.stereotype.Controller; |
| 38 | +import org.springframework.ui.ModelMap; |
| 39 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 40 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 41 | +import org.springframework.web.client.RestTemplate; |
| 42 | + |
| 43 | +@Controller |
| 44 | +@PreAuthorize("hasRole('ROLE_CUSTODIAN')") |
| 45 | +public class AdministratorController extends BaseController { |
| 46 | + |
| 47 | + @Autowired |
| 48 | + private RetailCustomerService service; |
| 49 | + |
| 50 | + @Autowired |
| 51 | + @Qualifier("restTemplate") |
| 52 | + private RestTemplate restTemplate; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private ResourceService resourceService; |
| 56 | + |
| 57 | + @Autowired |
| 58 | + private ImportService importService; |
| 59 | + |
| 60 | + public void setService(RetailCustomerService service) { |
| 61 | + this.service = service; |
| 62 | + } |
| 63 | + |
| 64 | + @RequestMapping(value = Routes.ROOT_SERVICE_STATUS, method = RequestMethod.GET) |
| 65 | + public String showServiceStatus(ModelMap model) { |
| 66 | + |
| 67 | + ApplicationInformation applicationInformation = resourceService.findById(1L, ApplicationInformation.class); |
| 68 | + String statusUri = applicationInformation.getAuthorizationServerAuthorizationEndpoint() + "/ReadServiceStatus"; |
| 69 | + // not sure this will work w/o the right seed information |
| 70 | + // |
| 71 | + Authorization authorization = resourceService.findByResourceUri(statusUri, Authorization.class); |
| 72 | + RetailCustomer retailCustomer = authorization.getRetailCustomer(); |
| 73 | + |
| 74 | + String accessToken = authorization.getAccessToken(); |
| 75 | + String serviceStatus = "OK"; |
| 76 | + |
| 77 | + try { |
| 78 | + |
| 79 | + HttpHeaders requestHeaders = new HttpHeaders(); |
| 80 | + requestHeaders.set("Authorization", "Bearer " + accessToken); |
| 81 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 82 | + HttpEntity<?> requestEntity = new HttpEntity(requestHeaders); |
| 83 | + |
| 84 | + // get the subscription |
| 85 | + HttpEntity<String> httpResult = restTemplate.exchange(statusUri, HttpMethod.GET, requestEntity, String.class); |
| 86 | + |
| 87 | + // import it into the repository |
| 88 | + ByteArrayInputStream bs = new ByteArrayInputStream(httpResult.getBody() |
| 89 | + .toString().getBytes()); |
| 90 | + |
| 91 | + importService.importData(bs, retailCustomer.getId()); |
| 92 | + |
| 93 | + List<EntryType> entries = importService.getEntries(); |
| 94 | + |
| 95 | + // not sure where the <feed> description is going to be .... |
| 96 | + |
| 97 | + |
| 98 | + } catch (Exception e) { |
| 99 | + // nothing there, so log the fact and move on. It will |
| 100 | + // get imported later. |
| 101 | + e.printStackTrace(); |
| 102 | + } |
| 103 | + model.put("serviceStatus", serviceStatus); |
| 104 | + |
| 105 | + return "/custodian/datacustodian/showservicestatus"; |
| 106 | + } |
| 107 | + |
| 108 | +} |
| 109 | + |
0 commit comments