2017-05-31 97 views
0

我已經被賦予將WebLogic 12.1.3的應用程序移動到Payara 4.1的任務,並且遇到了一個問題,我覺得我更加或更少在排除故障的最後。 我們有一個EJB(一種無狀態bean),它有兩種方法,一種調用Google Maps Directions API,一種調用Google Maps Geocoding API,兩種方法都使用相同的證書和Google的Googles客戶端庫。這兩種方法都可以在WebLogic上正常運行,但在切換到Payara後,使用Directions API的方法給我一個錯誤。這裏的堆棧跟蹤的相關部分:Google Maps API(Directions)在Payara中引發403錯誤,而不是在WebLogic中

java.io.IOException: Server Error: 403 Forbidden 
    at com.google.maps.internal.OkHttpPendingResult.parseResponse(OkHttpPendingResult.java:258) 
    at com.google.maps.internal.OkHttpPendingResult.await(OkHttpPendingResult.java:167) 
    at com.google.maps.PendingResultBase.await(PendingResultBase.java:56) 
    at com.somecompany.integration.GoogleDirectionsIntegration.getDirections(GoogleDirectionsIntegration.java:XXX) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 

因此,地理編碼法仍然有效,這兩個平臺上,而是試圖在調用地圖API時,我收到了403回從谷歌表明我的憑據搞砸了,但是地理編碼調用可以使用相同的憑據。代碼沒有以任何方式從一個平臺切換到另一個平臺。

什麼是更令人困惑的是,如果我抓住從日誌中調用谷歌的實際URL,並嘗試在我的瀏覽器,即粘貼「https://maps.googleapis.com/maps/api/directions/json?client=gme-company&mode=driving&arrival_time=1435825037&o rigin =某處&目的地=某處+其他& alternat 艾夫斯=假的&簽名= nfre3XYZ2kmuDX8Qibce87ZFKQQ =「到Chrome,它的工作原理。我得到了Google的正確答案。 (順便說一下,這些不是我使用的實際憑據或來源和目的地,他們已經「匿名」:-))。我還檢查了這個URL(由客戶端庫構建)在兩個平臺上都運行相同,並且在Google的開發者頁面上使用了URL簽名調試器,但無濟於事。我的憑證應該沒有問題。

我真的在這裏結束了,花了幾天的時間排除故障並在網上搜索而沒有找到解決方案。

這不是問題那麼多,但我沒有寫這個代碼我自己,和誰做課程的人並不在這裏了反正工作:-P

,這裏的代碼(有點匿名):

@Stateless 
public class GoogleDirectionsIntegration { 

private static final Logger LOGGER = Logger.getLogger(GoogleDirectionsIntegration.class.getName()); 

private GeoApiContext context = null; 

/** 
* Initializer 
*/ 
@PostConstruct 
public void init() { 
    LOGGER.log(Level.INFO, "initiating {0}", this.getClass().getSimpleName()); 
    this.context = new GeoApiContext().setEnterpriseCredentials("gme-company", "companyGoogleCryptographicSecret"); 
    this.context.setReadTimeout(1, TimeUnit.SECONDS) 
      .setRetryTimeout(1, TimeUnit.SECONDS) 
      .setConnectTimeout(1, TimeUnit.SECONDS) 
      .setWriteTimeout(1, TimeUnit.SECONDS); 
    OkHttpRequestHandler okHttpRequestHandler = null; 
    OkHttpClient okHttpClient = null; 

    try { 
     Field requestField = this.context.getClass().getDeclaredField("requestHandler"); 
     requestField.setAccessible(true); 
     okHttpRequestHandler = (OkHttpRequestHandler) requestField.get(this.context); 
     Field f = okHttpRequestHandler.getClass().getDeclaredField("client"); 
     f.setAccessible(true); 
     okHttpClient = (OkHttpClient) f.get(okHttpRequestHandler); 
    } catch (NoSuchFieldException | SecurityException | IllegalAccessException | IllegalArgumentException e) { 
     throw new IllegalStateException("Failed to create SSL context", e); 
    } 

    SSLContext sslCtx = this.getSslContext(); 

    if (sslCtx != null && okHttpClient != null) { 
     SSLSocketFactory sslSocketFactory = sslCtx.getSocketFactory(); 
     okHttpClient.setSslSocketFactory(sslSocketFactory); 
    } 
} 

private SSLContext getSslContext() { 
    TrustManager[] tm = new TrustManager[] { 
      new CustomTrustManager() 
    }; 

    SSLContext sslContext = null; 
    try { 
     sslContext = SSLContext.getInstance("TLSv1.2"); 
     sslContext.init(null, tm, new SecureRandom()); 
    } catch (NoSuchAlgorithmException | KeyManagementException ex) { 
     throw new IllegalStateException("Failed to create SSL context", ex); 
    } 
    return sslContext; 
} 

public DirectionsRoute getDirections(final String origin, final String destination, final DistanceUnit distanceUnit, 
     @Nullable TransportMode mode, @NotNull Instant arrivalTime) throws NotFoundException { 
    TransportMode actualMode = mode == null ? TransportMode.CAR : mode; 
    DirectionsRoute[] directionsRoutes; 

    DirectionsApiRequest directionsApiRequest = DirectionsApi.getDirections(this.context, origin, destination); 
    directionsApiRequest.arrivalTime(new Instant(arrivalTime)); 
    directionsApiRequest.alternatives(false); 
    directionsApiRequest.mode(this.toTravelMode(actualMode)); 

    try { 
     DirectionsResult res = directionsApiRequest.await(); // THIS IS WHERE IT BREAKS! 
     directionsRoutes = res.routes; 
    } catch (Exception e) { 
     LOGGER.log(Level.WARNING, e.getMessage(), e); 
     throw new NotFoundException(e.getMessage()); 
    } 

    if (directionsRoutes.length != 1) { 
     throw new NotFoundException("Failed to fetch valid directions"); 
    } 

    return directionsRoutes[0]; 
} 

public void getAddress(LatLng startLocation, Location location, boolean cacheOverride) throws Exception { 
    com.google.maps.model.LatLng gLatLng = new com.google.maps.model.LatLng(startLocation.getLat(), startLocation.getLng()); 
    GeocodingApiRequest geocodingApiRequest = GeocodingApi.reverseGeocode(this.context, gLatLng); 
    GeocodingResult[] geocodingResults; 
    geocodingResults = geocodingApiRequest.await(); 
    if (0 < geocodingResults.length) { 
     //.. Code that does stuff with the result.. 
    } else { 
     LOGGER.log(Level.WARNING, "Received empty results from Google reverse geocode for [{0}].", startLocation); 
    } 
} 

} 
+0

您是否爲密鑰啓用了Google Maps Directions Web服務? (在[Google API控制檯](https://console.developers.google.com/)) – geocodezip

+0

@geocodezip顯然,因爲當應用程序在WebLogic上運行時,一切正常。 – FighterHayabusa

+0

它不清楚它是相同的域名/網絡服務器(是嗎?)。 – geocodezip

回答

0

所以我解決了它。問題不在代碼中,而是在依賴關係中,或者是依賴於某個依賴關係 - OkHttp。我只是改變了版本,現在它的作品。