
JAXB
Publicado por Rocio (22 intervenciones) el 03/06/2015 18:18:18
HOLA !!
BUENOS DIAS QUISIERA QUE ME AYUDEN DESDE HACE VARIOS DIAS QUE ESTOY BATALLANDO
MI PROBLEMA ES COMO ENVIOS MIS DATOS DE CONSULTA DE UNA BASE DE DATOS A UN htttpcliente o url
ESTOY USANDO LA API QUE ME PROPORCIONO UPS Y ES UN POCO DIFICL
PUEDO ENVIAR UNO Y OBTENER LA RESPUESTA PERO QUIERO ENVIAR TODOS DE MANERA AUTOMATICA QUI ESTA MI CODIGO.
Y ESTOS SON LOS DATOS QUE QUIERO NEVIAR TODO JUNTOS ESTOS SON OBTENIDOS DE MI BASE DE DATOS. SERIAN TAN AMABLES DE AYUDARME ESTOY MUY CONFUNDIDA SOY NUEVA EN ESTO Y SI ME URGUE TERMINARLO :(
ESTO SOLO ALGUNOS !!!
1Z6X7319034225830
1Z6X7319034110596
1Z6X7319034197562
1Z6X7319034185658
BUENOS DIAS QUISIERA QUE ME AYUDEN DESDE HACE VARIOS DIAS QUE ESTOY BATALLANDO
MI PROBLEMA ES COMO ENVIOS MIS DATOS DE CONSULTA DE UNA BASE DE DATOS A UN htttpcliente o url
ESTOY USANDO LA API QUE ME PROPORCIONO UPS Y ES UN POCO DIFICL
PUEDO ENVIAR UNO Y OBTENER LA RESPUESTA PERO QUIERO ENVIAR TODOS DE MANERA AUTOMATICA QUI ESTA MI CODIGO.
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.ups.xolt.codesamples;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Properties;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.ups.xolt.codesamples.accessrequest.jaxb.AccessRequest;
import com.ups.xolt.codesamples.request.jaxb.Request;
import com.ups.xolt.codesamples.request.jaxb.TrackRequest;
import com.ups.xolt.codesamples.response.jaxb.TrackResponse;
public class PODPTM {
private static final String LICENSE_NUMBER = "accesskey";
private static final String USER_NAME = "username";
private static final String PASSWORD = "password";
private static final String ENDPOINT_URL="url";
private static final String OUT_FILE_LOCATION = "out_file_location";
private static Properties props = null;
private static String description = null;
static {
props = new Properties();
try{
props.load(new FileInputStream("./build.properties"));
}catch (Exception e) {
description = e.toString();
updateResultsToFile(description);
e.printStackTrace();
}
}
public void principal(){
StringWriter strWriter =null;
for (int f=0; f<5; f++)
try {
//Create JAXBContext and marshaller for AccessRequest object
JAXBContext accessRequestJAXBC = JAXBContext.newInstance(AccessRequest.class.getPackage().getName() );
Marshaller accessRequestMarshaller = accessRequestJAXBC.createMarshaller();
com.ups.xolt.codesamples.accessrequest.jaxb.ObjectFactory accessRequestObjectFactory = new com.ups.xolt.codesamples.accessrequest.jaxb.ObjectFactory();
AccessRequest accessRequest = accessRequestObjectFactory.createAccessRequest();
populateAccessRequest(accessRequest); //esto ejecuta el metodo donde ingreesas el usuario
//Create JAXBContext and marshaller for TrackRequest object
JAXBContext trackRequestJAXBC = JAXBContext.newInstance(TrackRequest.class.getPackage().getName() );
Marshaller trackRequestMarshaller = trackRequestJAXBC.createMarshaller();
com.ups.xolt.codesamples.request.jaxb.ObjectFactory requestObjectFactory = new com.ups.xolt.codesamples.request.jaxb.ObjectFactory();
TrackRequest trackRequest = requestObjectFactory.createTrackRequest();
populateTrackRequest(trackRequest);// este metodo realializa la coneccion y escritura de tracking
//Get String out of access request and track request objects
strWriter = new StringWriter();
accessRequestMarshaller.marshal(accessRequest, strWriter);
trackRequestMarshaller.marshal(trackRequest, strWriter);
strWriter.flush();
strWriter.close();
System.out.println("Request: " + strWriter.getBuffer().toString());
String strResults =contactService(strWriter.getBuffer().toString());
//Parse response object
JAXBContext trackResponseJAXBC = JAXBContext.newInstance(TrackResponse.class.getPackage().getName());
Unmarshaller trackUnmarshaller = trackResponseJAXBC.createUnmarshaller();
ByteArrayInputStream input = new ByteArrayInputStream(strResults.getBytes());
Object objResponse = trackUnmarshaller.unmarshal(input);
TrackResponse trackResponse = (TrackResponse)objResponse;
System.out.println("Response Status: " + trackResponse.getResponse().getResponseStatusCode());
System.out.println("Response Status Description: " + trackResponse.getResponse().getResponseStatusDescription());
//System.out.println("Inquiry Number: " + trackResponse.getShipment().get(0).getInquiryNumber().getValue());
// System.out.println("Current Status Code: " + trackResponse.getShipment().get(0).getCurrentStatus().getCode());
//System.out.println("Current Status Description: " + trackResponse.getShipment().get(0).getCurrentStatus().getDescription());
updateResultsToFile(strResults);
} catch (Exception e) {
description = e.toString();
updateResultsToFile(description);
e.printStackTrace();
} finally{
try{
if(strWriter != null){
strWriter.close();
strWriter = null;
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
public static String contactService(String xmlInputString) throws Exception{
String outputStr = null;
OutputStream outputStream = null;
try {
URL url = new URL(props.getProperty(ENDPOINT_URL));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
System.out.println("Client established connection with " + url.toString());
//Setup HTTP POST parameters
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
outputStream = connection.getOutputStream();
outputStream.write(xmlInputString.getBytes());
outputStream.flush();
outputStream.close();
System.out.println("Http status = " +connection.getResponseCode() + " " +connection.getResponseMessage());
outputStr = readURLConnection(connection);
connection.disconnect();
} catch (Exception e) {
System.out.println("Error sending data to server");
e.printStackTrace();
throw e;
} finally {
if(outputStream != null){
outputStream.close();
}
}
return outputStr;
}
/**
* This method read all of the data from a URL connection to a String
*/
public static String readURLConnection(URLConnection uc) throws Exception {
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(uc.getInputStream())); // lee la salida del servidor
int letter =0;
while ((letter = reader.read())!=-1)
buffer.append((char)letter);
reader.close();
} catch (Exception e) {
System.out.println("Could not read from URL: " + e.toString());
throw e;
} finally {
if(reader != null){
reader.close();
reader = null;
}
}
return buffer.toString();
}
/**
* Populates the access request object.
* @param accessRequest
*/
public static void populateAccessRequest(AccessRequest accessRequest){
accessRequest.setAccessLicenseNumber(props.getProperty(LICENSE_NUMBER));
accessRequest.setUserId(props.getProperty(USER_NAME));
accessRequest.setPassword(props.getProperty(PASSWORD));
}
public static void populateTrackRequest(TrackRequest trackRequest ){
Request request = new Request();
List<String> optoinsList = request.getRequestOption();
request.setRequestAction("Track");
trackRequest.setRequest(request); //If the request option here is of 2 ~ 15, then Signature tracking must validate the rights to signature tracking.
trackRequest.setTrackingNumber("1Z6X73190341928947");
trackRequest.setIncludeFreight("01");
}
/*
* This method updates the XOLTResult.xml file with the received status and description
* @param statusCode
* @param description
*/
public static void updateResultsToFile(String response){
BufferedWriter bw = null;
try{
File outFile = new File(props.getProperty(OUT_FILE_LOCATION));
System.out.println("Output file deletion status: " + outFile.delete());
outFile.createNewFile();
System.out.println("Output file location: " + outFile.getAbsoluteFile());
bw = new BufferedWriter(new FileWriter(outFile));
bw.write(response);
bw.close();
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if (bw != null){
bw.close();
bw = null;
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
Y ESTOS SON LOS DATOS QUE QUIERO NEVIAR TODO JUNTOS ESTOS SON OBTENIDOS DE MI BASE DE DATOS. SERIAN TAN AMABLES DE AYUDARME ESTOY MUY CONFUNDIDA SOY NUEVA EN ESTO Y SI ME URGUE TERMINARLO :(
ESTO SOLO ALGUNOS !!!
1Z6X7319034225830
1Z6X7319034110596
1Z6X7319034197562
1Z6X7319034185658
Valora esta pregunta


0