Skip to main content
summaryrefslogtreecommitdiffstats
blob: 380b0e592ec9f14666017550ae77f7b8609e64a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.manager.servlet;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.osee.framework.core.enums.CoreTranslatorId;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.model.OseeImportModelRequest;
import org.eclipse.osee.framework.core.model.OseeImportModelResponse;
import org.eclipse.osee.framework.core.server.ISessionManager;
import org.eclipse.osee.framework.core.server.SecureOseeHttpServlet;
import org.eclipse.osee.framework.core.translation.IDataTranslationService;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.OrcsTypes;

/**
 * @author Roberto E. Escobar
 */
public class OseeModelServlet extends SecureOseeHttpServlet {

   private static final long serialVersionUID = -2639113870500561780L;

   private final OrcsApi orcsApi;
   private final IDataTranslationService dataTransalatorService;

   public OseeModelServlet(Log logger, ISessionManager sessionManager, IDataTranslationService dataTransalatorService, OrcsApi orcsApi) {
      super(logger, sessionManager);
      this.dataTransalatorService = dataTransalatorService;
      this.orcsApi = orcsApi;
   }

   private OrcsTypes getOrcsTypes() {
      return orcsApi.getOrcsTypes(null);
   }

   @Override
   protected void checkAccessControl(HttpServletRequest request) throws OseeCoreException {
      if (!request.getMethod().equalsIgnoreCase("GET")) {
         super.checkAccessControl(request);
      }
   }

   private void handleError(HttpServletResponse resp, String request, Throwable th) throws IOException {
      getLogger().error(th, "Osee Cache request error: [%s]", request);
      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      resp.setContentType("text/plain");
      resp.getWriter().write(Lib.exceptionToString(th));
      resp.getWriter().flush();
      resp.getWriter().close();
   }

   @Override
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      try {
         getOrcsTypes().writeTypes(output).call();
         resp.setStatus(HttpServletResponse.SC_ACCEPTED);
         resp.setContentType("text/plain");
         resp.setCharacterEncoding("UTF-8");

         Lib.inputStreamToOutputStream(new ByteArrayInputStream(output.toByteArray()), resp.getOutputStream());
      } catch (Exception ex) {
         handleError(resp, req.getQueryString(), ex);
      }
   }

   @Override
   protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      try {
         final OseeImportModelRequest modelRequest =
            dataTransalatorService.convert(req.getInputStream(), CoreTranslatorId.OSEE_IMPORT_MODEL_REQUEST);

         IResource resource = new IResource() {

            @Override
            public InputStream getContent() throws OseeCoreException {
               InputStream inputStream = null;
               try {
                  inputStream = new ByteArrayInputStream(modelRequest.getModel().getBytes("UTF-8"));
               } catch (UnsupportedEncodingException ex) {
                  OseeExceptions.wrapAndThrow(ex);
               }
               return inputStream;
            }

            @Override
            public URI getLocation() {
               try {
                  String modelName = modelRequest.getModelName();
                  if (!modelName.endsWith(".osee")) {
                     modelName += ".osee";
                  }
                  return new URI("osee:/" + modelName);
               } catch (URISyntaxException ex) {
                  getLogger().error(ex, "Error creating location URI for model import");
               }
               return null;
            }

            @Override
            public String getName() {
               return modelRequest.getModelName();
            }

            @Override
            public boolean isCompressed() {
               return false;
            }

         };

         OseeImportModelResponse modelResponse = new OseeImportModelResponse();

         getOrcsTypes().loadTypes(resource).call();

         resp.setStatus(HttpServletResponse.SC_ACCEPTED);
         resp.setContentType("text/xml");
         resp.setCharacterEncoding("UTF-8");

         InputStream inputStream =
            dataTransalatorService.convertToStream(modelResponse, CoreTranslatorId.OSEE_IMPORT_MODEL_RESPONSE);
         Lib.inputStreamToOutputStream(inputStream, resp.getOutputStream());
      } catch (Exception ex) {
         handleError(resp, req.toString(), ex);
      }
   }
}

Back to the top