Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7350afdbd11a5a993bf2196fef529744dde72872 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
 * 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.ats;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLConnection;
import java.util.Collection;
import java.util.logging.Level;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.core.util.Conditions;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.manager.servlet.internal.Activator;
import org.eclipse.osee.framework.resource.management.IResource;
import org.eclipse.osee.framework.resource.management.util.Resource;
import org.w3c.dom.Node;

/**
 * @author Roberto E. Escobar
 */
public class XmlMessage {

   public XmlMessage() {
   }

   public void sendError(HttpServletResponse response, String name, Throwable ex) {
      String errorMessage = Lib.exceptionToString(ex);
      OseeLog.log(Activator.class, Level.SEVERE, errorMessage, ex);
      try {
         StringWriter writer = new StringWriter();
         createXmlErrorMessage(writer, "0", errorMessage);
         IResource resource = new StreamResource(name, writer.toString());
         sendMessage(response, name, resource, false);
      } catch (Throwable ex1) {
         OseeLog.log(Activator.class, Level.SEVERE, ex1.toString(), ex1);
      }
   }

   public void sendMessage(HttpServletResponse response, String name, String status, Collection<Node> nodes) {
      try {
         StringWriter writer = new StringWriter();
         createXmlMessage(writer, "0", false, nodes);
         IResource resource = new StreamResource(name, writer.toString());
         sendMessage(response, name, resource, true);
      } catch (Throwable ex) {
         sendError(response, name, ex);
      }
   }

   private void createXmlErrorMessage(Writer output, String status, String... messages) throws OseeCoreException {
      createXmlMessage(output, status, true, null, messages);
   }

   private void createXmlMessage(Writer output, String status, boolean isError, Collection<Node> nodes, String... messages) throws OseeCoreException {
      boolean hasNodes = nodes != null && !nodes.isEmpty();
      boolean hasMessages = messages != null && messages.length > 0;

      Conditions.checkExpressionFailOnTrue(hasMessages && hasNodes, "Cannot have messages and nodes");

      XMLOutputFactory factory = XMLOutputFactory.newInstance();
      try {
         XMLStreamWriter writer = factory.createXMLStreamWriter(output);
         writer.writeStartDocument("UTF-8", "1.0");
         writer.writeStartElement("response");

         writer.writeStartElement("status");
         writer.writeCharacters(status);
         writer.writeEndElement();

         if (!isError) {
            //TODO: update if we want to return a partial set
            writer.writeStartElement("startRow");
            writer.writeCharacters("0");
            writer.writeEndElement();

            //TODO: update if we want to return a partial set
            writer.writeStartElement("endRow");
            int size = 0;
            if (hasNodes) {
               size = nodes.size();
            }
            if (hasMessages) {
               size = messages.length;
            }
            writer.writeCharacters(String.valueOf(size));
            writer.writeEndElement();

            writer.writeStartElement("totalRows");
            writer.writeCharacters(String.valueOf(size));
            writer.writeEndElement();
         }

         if (isError) {
            writer.writeStartElement("errors");
         } else {
            writer.writeStartElement("data");
         }

         try {
            if (hasNodes) {
               XmlUtil.serialize(writer, nodes);
            }
            if (hasMessages) {
               for (String message : messages) {
                  writer.writeStartElement("message");
                  writer.writeCharacters(message);
                  writer.writeEndElement();
               }
            }
         } finally {
            writer.writeEndElement();
         }
         writer.writeEndElement();
         writer.writeEndDocument();
      } catch (XMLStreamException ex) {
         OseeExceptions.wrapAndThrow(ex);
      }
   }

   public void sendMessage(HttpServletResponse response, String name, IResource resource, boolean isErrorSendingAllowed) {
      InputStream inputStream = null;
      try {
         inputStream = resource.getContent();

         response.setStatus(HttpServletResponse.SC_OK);
         response.setContentLength(inputStream.available());
         response.setCharacterEncoding("ISO-8859-1");

         String mimeType = null;
         if (name.endsWith("css")) {
            mimeType = "text/css";
         }
         if (mimeType == null) {
            mimeType = URLConnection.guessContentTypeFromStream(inputStream);
            if (mimeType == null) {
               mimeType = URLConnection.guessContentTypeFromName(resource.getLocation().toString());
               if (mimeType == null) {
                  mimeType = "application/*";
               }
            }
         }
         response.setContentType(mimeType);

         boolean isReportDoc = false;
         try {
            String location = resource.getLocation().toASCIIString();
            isReportDoc = location.contains("changeReports");
         } catch (Exception ex) {
            // nothing here;
         }

         if (!mimeType.startsWith("text") && resource.isCompressed() || isReportDoc) {
            response.setHeader("Content-Disposition", "attachment; filename=" + resource.getName());
         }
         Lib.inputStreamToOutputStream(inputStream, response.getOutputStream());
      } catch (Exception ex) {
         if (isErrorSendingAllowed) {
            sendError(response, name, ex);
         } else {
            OseeLog.log(Activator.class, Level.SEVERE, ex);
         }
      } finally {
         Lib.close(inputStream);
      }
   }

   private final static class StreamResource extends Resource {

      private final ByteArrayInputStream inputStream;
      private final String name;

      public StreamResource(String name, String data) throws UnsupportedEncodingException {
         this(name, false, new ByteArrayInputStream(data.getBytes("ISO-8859-1")));
      }

      public StreamResource(String name, boolean isCompressed, ByteArrayInputStream inputStream) {
         super(null, isCompressed);
         this.name = name;
         this.inputStream = inputStream;
      }

      @Override
      public InputStream getContent() {
         return inputStream;
      }

      @Override
      public String getName() {
         return name;
      }
   }
}

Back to the top